admin管理员组文章数量:1333451
I am trying to update a field in an object in my array.
My object (simplified) looks something like this:
{
"readableStructure": [
{
"ID": "1",
"Name": "Name 1",
"Description": "Adding, updating or removing users",
"IconName": "fas fa-users-cog"
},
{
"ID": "2",
"Name": "Name 2",
"Description": "Views overview",
"IconName": "fas fa-street-view"
},
{
"ID": "3",
"Name": "Name 3",
"Description": "Improvements to apps",
"IconName": "fas fa-broadcast-tower"
},
{
"ID": "4",
"Name": "Name 4",
"Description": "Managing badges",
"IconName": "fas fa-id-badge"
},
{
"ID": "5",
"Name": "Name 5",
"Description": "Art",
"IconName": "fas fa-hand-holding-heart"
}
]
}
and I call it like this:
prepareRepositories(this.props.primarySearchRepositories);
I can have several similar repos and I loop through them. And for each item in the array readableStructure
I am trying to add a property called QSString
using the following code.
prepareRepositories(repositories) {
const repos = repositories.map((repo, index) => {
const searchableColumns = ['Name', 'Description'];
const newRepo = [];
newRepo[0] = {};
newRepo[0].readableStructure = [];
newRepo[0].readableStructure = repo[0].readableStructure.map((item) => {
item.QSString = this.getSearchableString(item, searchableColumns) || 'blaha';
return item;
});
return newRepo;
});
return repos;
}
getSearchableString = (base, searchableColumns) => {
let searchables = [];
Object.keys(base).forEach((key) => {
if ((searchableColumns.indexOf(key) > -1) && (base[key] !== null) && (typeof base[key] !== 'object')) {
searchables.push(base[key]);
}
});
const searchableString = searchables.join(' ');
return searchableString;
}
However, when I do this the original object is modified and the QSString
property get set to the same thing for every repo.
What am I doing wrong?
I am trying to update a field in an object in my array.
My object (simplified) looks something like this:
{
"readableStructure": [
{
"ID": "1",
"Name": "Name 1",
"Description": "Adding, updating or removing users",
"IconName": "fas fa-users-cog"
},
{
"ID": "2",
"Name": "Name 2",
"Description": "Views overview",
"IconName": "fas fa-street-view"
},
{
"ID": "3",
"Name": "Name 3",
"Description": "Improvements to apps",
"IconName": "fas fa-broadcast-tower"
},
{
"ID": "4",
"Name": "Name 4",
"Description": "Managing badges",
"IconName": "fas fa-id-badge"
},
{
"ID": "5",
"Name": "Name 5",
"Description": "Art",
"IconName": "fas fa-hand-holding-heart"
}
]
}
and I call it like this:
prepareRepositories(this.props.primarySearchRepositories);
I can have several similar repos and I loop through them. And for each item in the array readableStructure
I am trying to add a property called QSString
using the following code.
prepareRepositories(repositories) {
const repos = repositories.map((repo, index) => {
const searchableColumns = ['Name', 'Description'];
const newRepo = [];
newRepo[0] = {};
newRepo[0].readableStructure = [];
newRepo[0].readableStructure = repo[0].readableStructure.map((item) => {
item.QSString = this.getSearchableString(item, searchableColumns) || 'blaha';
return item;
});
return newRepo;
});
return repos;
}
getSearchableString = (base, searchableColumns) => {
let searchables = [];
Object.keys(base).forEach((key) => {
if ((searchableColumns.indexOf(key) > -1) && (base[key] !== null) && (typeof base[key] !== 'object')) {
searchables.push(base[key]);
}
});
const searchableString = searchables.join(' ');
return searchableString;
}
However, when I do this the original object is modified and the QSString
property get set to the same thing for every repo.
What am I doing wrong?
Share Improve this question asked Jun 5, 2018 at 9:24 WinterWinter 2,5152 gold badges23 silver badges30 bronze badges 3- Have you tried debugging? – Jorge.V Commented Jun 5, 2018 at 9:27
-
item.QSString = this.getSearchableString ...
-- this statement causes the issue. You're modifying the object in-place. What did you expect otherwise? – 31piy Commented Jun 5, 2018 at 9:27 -
I am using react, hence calling the function
getSearchableString
withthis
. – Winter Commented Jun 5, 2018 at 9:30
2 Answers
Reset to default 9Array.map does not change the original array, i.e. creates a new Array. However, any object in the Array continues to hold the same reference, hence, any change in object even inside map function will update the object in original array.
You need to create a copy for the object.
item = JSON.parse(JSON.stringify(item))
using this wont mutate the original object
JSON.parse(JSON.stringify(your_object))
本文标签: javascriptarraymap is mutating my original arrayStack Overflow
版权声明:本文标题:javascript - array.map is mutating my original array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742353982a2459048.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论