0
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

Mohammad
21.5k16 gold badges58 silver badges86 bronze badges
asked Nov 15, 2018 at 16:16
1
  • 1
    What is your specific property, which property in the array are you looking for? Commented Nov 15, 2018 at 16:22

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Since delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approach
0

How 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

Comments

0

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})));

answered Nov 15, 2018 at 16:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.