Say I have an array of objects
var result = [{
"sss": "sssssss",
"yyy": "ssdsdsds",
"www": "1212121",
"Group": "Mango"
}, {
"sss": "sssssss",
"yyy": "ssdsdsds",
"www": "1212121",
"Group": "Mango"
}]
I want a final result like :
var result ={
"Mango" : [
{
"sss": "sssssss",
"yyy": "ssdsdsds",
"www": "1212121"
}, {
"sss": "sssssss",
"yyy": "ssdsdsds",
"www": "1212121"
}
]
}
So I did this
var obj = [];
for(var i = 0; i < result.length; i++ ){
if(obj[result[i].Group] && obj[result[i].Group].constructor === Array ){
}else{
obj[result[i].Group] = [];
// ################### PROBLEM ==========
var x = result[i];
delete x.Group;
console.log(result[i]);
// ################### ==================
obj[result[i].Group].push(result[i]); // error: result[i].Group is undefined
}
}
the code "delete x.Group"
should delete property "Group"
from var "x"
But actually the console.log(result[i]);
shows
the property "Group"
is deleted from
result[i]
(which is original). HOW?
Looks like variable x
is reference to the original result[i]
, this am not sure.
How the deleting a property from the copy of the object is affecting the original. How to overcome this issue?