var obo = [{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}, {
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}, {
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}
]
I want to loop through and return the array. i have tried splice its not working
var obo = [{
"userid": "tomi",
"location": "kwara"
},
{
"userid": "tomi",
"location": "kwara"
},
{
"userid": "tomi",
"location": "kwara"
}
]
Am trying to populate the array so as not to have parcelid in it
-
1What is your specific property, which property in the array are you looking for?squeekyDave– squeekyDave2018年11月15日 16:22:15 +00:00Commented Nov 15, 2018 at 16:22
4 Answers 4
You can use .map() with some object destructuring:
var data = [
{"parcelId": "009", "userid": "tomi", "location": "kwara"},
{"parcelId": "009", "userid": "tomi", "location": "kwara"},
{"parcelId": "009", "userid": "tomi", "location": "kwara"}
];
var result = data.map(({userid, location, ...rest}) => ({userid, location}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
References:
answered Nov 15, 2018 at 16:20
Mohammad Usman
39.6k20 gold badges99 silver badges101 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You need to use .map()
obo = obo.map(function(item){
return {
userid: item.userid,
location: item.location
};
});
var obo = [
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}
];
obo = obo.map(function(item){
return {
userid: item.userid,
location: item.location
};
});
console.log(obo);
answered Nov 15, 2018 at 16:20
Mohammad
21.5k16 gold badges58 silver badges86 bronze badges
1 Comment
charlietfl
Since
delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approachHow about that with Array.prototype.map()?
var obo = [{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
},
{
"parcelId": "009",
"userid": "tomi",
"location": "kwara"
}
]
var expected = [];
obo.map((elm, ind) => {
expected.push({
"userid": elm.userid,
"location": elm.location
})
})
console.log(expected)
answered Nov 15, 2018 at 16:23
A l w a y s S u n n y
38.8k9 gold badges69 silver badges120 bronze badges
Comments
You can try something like this
obo = obo.map(el => {
let obj = {
userid: el.userid,
location: el.location
};
return obj;
});
or for a one liner, you can do this
obo = obo.map(({userid,location})=>({userid,location})));
Comments
lang-js