我试图解析一个传入的json字符串并将新对象推送到该字符串。
我试过这样的:
addSetting(category) { console.log(category.value); //Console.log = [{"meta":"","value":""}] category.value = JSON.parse(category.value).push({meta: "", value: ""}); console.log(category.value); //Console.log = 2 },问题是category.value是2 ? 我期待2个json对象? 我在这里做错了什么?
I'm trying to parse an incoming json string and push a new object to that.
I've tried it like this:
addSetting(category) { console.log(category.value); //Console.log = [{"meta":"","value":""}] category.value = JSON.parse(category.value).push({meta: "", value: ""}); console.log(category.value); //Console.log = 2 },Problem is that category.value is 2? I would expect 2 json objects? What am I doing wrong here?
最满意答案
尝试:
addSetting(category) { category.value = JSON.parse(category.value); category.value.push({meta: "", value: ""}); console.log(category.value); }您正在使用push()不正确。 数组函数push()返回新数组的大小。
返回值
调用方法的对象的新长度属性。
ref: Array.prototype.push()MDN
由于数组的新大小为2 (在推送新元素之后),以下行不正确:
category.value = JSON.parse(category.value).push({meta: "", value: ""});在解析JSON之后,为变量category.value分配了push的返回值。
Try:
addSetting(category) { category.value = JSON.parse(category.value); category.value.push({meta: "", value: ""}); console.log(category.value); }You are using push() incorrectly. The array function push() returns the size of the new array.
Return value
The new length property of the object upon which the method was called.
ref:Array.prototype.push() MDN
As the new size of your array will be 2 (after pushing the new element) the following line was incorrect:
category.value = JSON.parse(category.value).push({meta: "", value: ""});As after JSON was parsed, the variable category.value was assigned the return value of push.
将新对象添加到传入的json字符串(Add new object to incoming json string)我试图解析一个传入的json字符串并将新对象推送到该字符串。
我试过这样的:
addSetting(category) { console.log(category.value); //Console.log = [{"meta":"","value":""}] category.value = JSON.parse(category.value).push({meta: "", value: ""}); console.log(category.value); //Console.log = 2 },问题是category.value是2 ? 我期待2个json对象? 我在这里做错了什么?
I'm trying to parse an incoming json string and push a new object to that.
I've tried it like this:
addSetting(category) { console.log(category.value); //Console.log = [{"meta":"","value":""}] category.value = JSON.parse(category.value).push({meta: "", value: ""}); console.log(category.value); //Console.log = 2 },Problem is that category.value is 2? I would expect 2 json objects? What am I doing wrong here?
最满意答案
尝试:
addSetting(category) { category.value = JSON.parse(category.value); category.value.push({meta: "", value: ""}); console.log(category.value); }您正在使用push()不正确。 数组函数push()返回新数组的大小。
返回值
调用方法的对象的新长度属性。
ref: Array.prototype.push()MDN
由于数组的新大小为2 (在推送新元素之后),以下行不正确:
category.value = JSON.parse(category.value).push({meta: "", value: ""});在解析JSON之后,为变量category.value分配了push的返回值。
Try:
addSetting(category) { category.value = JSON.parse(category.value); category.value.push({meta: "", value: ""}); console.log(category.value); }You are using push() incorrectly. The array function push() returns the size of the new array.
Return value
The new length property of the object upon which the method was called.
ref:Array.prototype.push() MDN
As the new size of your array will be 2 (after pushing the new element) the following line was incorrect:
category.value = JSON.parse(category.value).push({meta: "", value: ""});As after JSON was parsed, the variable category.value was assigned the return value of push.
发布评论