admin管理员组文章数量:1402797
I'm trying to conditionally add a property to an object in an array using .map(), but I seem to be missing something. My array of objects remains unchanged after the map is plete. All of this is the result of trying to resolve the ESLint error 'no-param-reassign' if that helps understand why I'm doing this.
I've made a rudimentary example of what I'm trying to acplish here: jsfiddle.
var aList = [{
Title: "Enders Game",
Rating: 8
},
{
Title: "Harry Potter",
Rating: 10
}];
console.log('before', aList);
aList.map(function(book) {
if(book.Title == "Enders Game"){
console.log({ ...book, pleted: true })
return { ...book, pleted: true };
}
console.log({ ...book, pleted: false })
return { ...book, pleted: false };
});
console.log('after', aList);
I'm trying to conditionally add a property to an object in an array using .map(), but I seem to be missing something. My array of objects remains unchanged after the map is plete. All of this is the result of trying to resolve the ESLint error 'no-param-reassign' if that helps understand why I'm doing this.
I've made a rudimentary example of what I'm trying to acplish here: jsfiddle.
var aList = [{
Title: "Enders Game",
Rating: 8
},
{
Title: "Harry Potter",
Rating: 10
}];
console.log('before', aList);
aList.map(function(book) {
if(book.Title == "Enders Game"){
console.log({ ...book, pleted: true })
return { ...book, pleted: true };
}
console.log({ ...book, pleted: false })
return { ...book, pleted: false };
});
console.log('after', aList);
Share
Improve this question
asked Feb 14, 2017 at 18:04
Tyler HTyler H
4701 gold badge7 silver badges18 bronze badges
1
-
3
.map()
doesn't mutate the original. It creates a new Array, which is returned, sovar newMap = aList.map(...
– user1106925 Commented Feb 14, 2017 at 18:07
2 Answers
Reset to default 6You could use Array#forEach
, because you are not using the result of Array#map
.
var aList = [{ Title: "Enders Game", Rating: 8 }, { Title: "Harry Potter", Rating: 10 }];
aList.forEach(function(book) {
book.pleted = book.Title == "Enders Game";
});
console.log(aList);
.as-console-wrapper { max-height: 100% !important; top: 0; }
For an all new array, you could use Array#map
and use the result of it. For adding a new property to an object, you could use Object.assign
, which mutates and returns the object at the first parameter.
var aList = [{ Title: "Enders Game", Rating: 8 }, { Title: "Harry Potter", Rating: 10 }],
newList = aList.map(book => Object.assign({}, book, {pleted: book.Title == "Enders Game"}));
console.log(aList);
console.log(newList);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I think, it could be written much cleaner and readable in that way.
const aList = [{ Title: 'Enders Game', Rating: 8 }, { Title: 'Harry Potter', Rating: 10 }];
const newList = aList.map((book) => (
{
...book,
pleted: book.Title === 'Enders Game',
}));
本文标签: javascriptAdding a new property to an Object that exists in an Arrayusing map()Stack Overflow
版权声明:本文标题:javascript - Adding a new property to an Object that exists in an Array, using .map() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744354035a2602215.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论