const words = [{ref : "a", list: "a & b & c & d", total: 0},
{ref : "a", list: "a & b & c & d", total: 0},
{ref : "b", list: "aa & bb & cc & dd", total: 0},
{ref : "c", list: "aaa & bbb & ccc & ddd", total: 0}];
var data = words.filter(item => {
if (item.ref == "a")
return item;
});
let result = data.map(o => {
o.list = o.list.split('&').map(key => ({
selected: false,
}))
return o;
});
console.log(words);
console.log(result);
- I have words array
- after filtering for
ref : a
i'm getting new arraydata
- but when i modifiy the
data
's object it is reflecting on main arraywords
's. which should not happen.
words
> Array [
Object { ref: "a", list: Array [Object { selected: false }, Object { selected: false }] },
Object { ref: "a", list: Array [Object { selected: false }, Object { selected: false }] },
Object { ref: "b", list: "aa & bb" },
Object { ref: "c", list: "aaa & bbb" }
]
result
> Array [
Object { ref: "a", list: Array [Object { selected: false }, Object { selected: false }] },
Object { ref: "a", list: Array [Object { selected: false }, Object { selected: false }] }
]
- In words you can see
ref b
andref c
is not modified - but
ref a
is changed. which should not happen in my case
* How to avoid alteration of original array words
? *