将多属性项推送到数组 JAVASCRIPT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9097366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pushing a multiple-attributed item to an array JAVASCRIPT
提问by Feeney
I have an array set out like this:
我有一个这样的数组:
var newCircles = [{
num: 0,
name: "title0",
x: 280,
y: 200,
color: "#555555",
r: 60
},
{
num: 1,
name: "title1",
x: 480,
y: 200,
color: "#555555",
r: 80
}];
And I'm trying to push new set of information like this:
我正在尝试推送这样的新信息集:
$(newCircles).push(', { num: "'+newCircles.length+'", name : "title "'+(newCircles.length)+'", x : "'+newCircles[chosenCircle].x+'", y : "'+newCircles[chosenCircle].y+'", color : "#7f38a7", r : "'+newCircles[chosenCircle].r+'" }');
But it's not working. Anyone have any suggestions?
但它不起作用。有人有什么建议吗?
回答by Sirko
you are pushing a string into the array. if you want to push another object into the array, then do so by
您正在将一个字符串推入数组。如果要将另一个对象推入数组,请执行以下操作
newCircles.push( {
num: newCircles.length,
name: 'title ' + newCircles.length,
x: newCircles[chosenCircle].x,
y: newCircles[chosenCircle].y,
color : "#7f38a7",
r: newCircles[chosenCircle].r
} );
回答by T.J. Crowder
You're using a string, but what you want to use is an object initializer(frequently called an object literal), just like you did when you initialized your array:
您使用的是字符串,但您想要使用的是对象初始值设定项(通常称为对象字面量),就像您在初始化数组时所做的那样:
newCircles.push({
num: newCircles.length, // Or you more likely want `newCircles.length + 1`
name: "title " + (newCircles.length), // Again, probably +1
x: newCircles[chosenCircle].x,
y: newCircles[chosenCircle].y,
color: "#7f38a7",
r: newCircles[chosenCircle].r
});
(There's also no reason for passing it through $()
.)
(也没有理由通过它$()
。)
As when you initialized your array, the tokens to the left of the :
are the property names, and the valueof the expressions on the right will be assigned as those properties' values.
与初始化数组时一样,左边的标记:
是属性名称,右边的表达式的值将被分配为这些属性的值。
回答by aziz punjani
Try this instead
试试这个
newCircles.push( { num: newCircles.length,
name: "title "+ newCircles.length,
x: newCircles[chosenCircle].x,
y: newCircles[chosenCircle].y,
color: "#7f38a7",
r: newCircles[chosenCircle].r
});
回答by James McLaughlin
Why is that a string? And why are you trying to wrap newCircles with jQuery?
为什么这是一个字符串?为什么要尝试用 jQuery 包装 newCircles?
newCircles.push({ num: newCircles.length, x: newCircles[chosenCircle].x, ... });
回答by Jamie Dixon
You don't need the leading comma when using the push method.
使用 push 方法时不需要前导逗号。
You're pushing the object directly onto the end of the array.
您将对象直接推到数组的末尾。
Also, there's no need to wrap the object up as a string.
此外,无需将对象包装为字符串。
$(newCircles).push({ num: "'+newCircles.length+'" [...]});
回答by schtever
Remove the $() around newCircles. You want the direct reference to that variable, not what jQuery returns when it looks it up.
删除 newCircles 周围的 $()。您希望直接引用该变量,而不是 jQuery 在查找时返回的内容。
回答by Dau
use as
用于
<script>
var newCircles = [
{
num: 0,
name: "title0",
x: 280,
y: 200,
color: "#555555",
r: 60
},
{
num: 1,
name: "title1",
x: 480,
y: 200,
color: "#555555",
r: 80
}
];
newCircles.push({"num":"10","name":"name","x":"100","y":"100","color":"color","r":"r"});
console.log(newCircles);
</script>
回答by eyurdakul
Don't worry, javascript may not work correctly if the syntax is totally wrong;
别担心,如果语法完全错误,javascript 可能无法正常工作;
newCircles.push({
"num":newCircles.length,
"a":"someval",
"b":"some other val"
});