[英]dojo array.push replaced the previous item
the following javascript returns personNames
as 以下javascript返回
personNames
作为
[{"name":"smith"},{"name":"smith"},{"name":"smith"}]
instead of 代替
[{"name":"john"},{"name":"doug"},{"name":"smith"}]
var personNames = []; var personName = {}; var persons = [{"firstname": "john", "id": "111"}, {"firstname": "doug", "id": "777"}, {"firstname": "smith", "id": "888"}]; if(persons.length > 0){ array.forEach(persons, function(person){ personName.name = person.firstname; personNames.push(personName); }); }
First as a way to grab names you can use Array.prototype.map
: 首先,可以使用
Array.prototype.map
作为获取名称的方法:
var persons = [{"firstname": "john", "id": "111"}, {"firstname": "doug", "id": "777"}, {"firstname": "smith", "id": "888"}]; var personNames = persons.map(p => ({ name: p.firstname })); console.log(personNames);
Then the problem with your code is that you created an object and every time you change the value of that and then push the same object to array again. 然后 ,代码的问题在于,您创建了一个对象,每次更改该对象的值,然后将同一对象再次推入数组。
I think you forget some thing about objects and referencing. 我认为您忘记了有关对象和引用的某些知识。 BTW this would be the correct implementation of yours:
顺便说一句,这将是您的正确实现:
var persons = [{"firstname": "john", "id": "111"}, {"firstname": "doug", "id": "777"}, {"firstname": "smith", "id": "888"}]; var personNames = []; persons.forEach(p => personNames.push({ name: p.firstname })); console.log(personNames);
Another thing to mention is that there is no need to check if there is any items in persons. 要提到的另一件事是,无需检查人员中是否有任何物品。
array.forEach(persons, function(person){
personName.name = person.firstname;
personNames.push(personName);
});
personName
refers to the same object in each iteration. personName
在每次迭代中都引用相同的对象。 JavaScript keeps a reference to that object in memory, so when you update it later on, it updates everywhere, including inside the array. JavaScript在内存中保留了对该对象的引用,因此当您以后对其进行更新时,它会在所有地方(包括数组内部)进行更新。
For your use-case, you need to re-initialize personName
each time: 对于您的用例,您每次需要重新初始化
personName
:
array.forEach(persons, function(person){
personName = {};
personName.name = person.firstname;
personNames.push(personName);
});
(Is the first line supposed to be persons.forEach(function(person){
?) (第一行应该是
persons.forEach(function(person){
吗?
Just use personName localy like that : 像这样使用personName语言环境:
var personNames = [];
var persons = [{"firstname": "john", "id": "111"}, {"firstname": "doug", "id": "777"}, {"firstname": "smith", "id": "888"}];
if(persons.length > 0) {
persons.forEach(element => {
var personName = {};
personName.name = element.firstname;
personNames.push(personName);
});
}
console.log(personNames); // [ { name: 'john' }, { name: 'doug' }, { name: 'smith' } ]
$array.forEach()
is not a function because array
is not defined. $array.forEach()
不是函数,因为未定义array
。 Use name of the array instead ie persons.forEach()
. persons.forEach()
。 personName
locally else you will be accessing the global instance of the variable which will return the last updated value. personName
,否则您将访问变量的全局实例,该实例将返回最近的更新值。 Check code snippet at JS Bin var personNames = []; var persons = [{"firstname": "john", "id": "111"}, {"firstname": "doug", "id": "777"}, {"firstname": "smith", "id": "888"}]; if(persons.length > 0){ persons.forEach(function(person){ //use personName locally var personName = {}; personName.name = person.firstname; personNames.push(personName); }); } console.log(personNames);
Try that 试试看
var personNames = [];
var persons = [{"firstname": "john", "id": "111"}, {"firstname": "doug", "id": "777"}, {"firstname": "smith", "id": "888"}];
for (i = 0; i < persons.length; i++) {
personNames.push({name:persons[i].firstname});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.