How to loop through an object array and output the array with a specific object property javascript
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
javascript arrays object
add a comment |
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
javascript arrays object
1
What is your specific property, which property in the array are you looking for?
– squeekyDave
Nov 15 '18 at 16:22
add a comment |
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
javascript arrays object
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
javascript arrays object
javascript arrays object
edited Nov 15 '18 at 16:58
Mohammad
15.8k123664
15.8k123664
asked Nov 15 '18 at 16:16
OpenwellOpenwell
12
12
1
What is your specific property, which property in the array are you looking for?
– squeekyDave
Nov 15 '18 at 16:22
add a comment |
1
What is your specific property, which property in the array are you looking for?
– squeekyDave
Nov 15 '18 at 16:22
1
1
What is your specific property, which property in the array are you looking for?
– squeekyDave
Nov 15 '18 at 16:22
What is your specific property, which property in the array are you looking for?
– squeekyDave
Nov 15 '18 at 16:22
add a comment |
4 Answers
4
active
oldest
votes
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:
- Array.prototype.map()
- Object Destructuring
add a comment |
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);
Sincedeletemutates original can do it in any loop. No real need to create new array using map() if using mutation approach
– charlietfl
Nov 15 '18 at 16:21
add a comment |
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)add a comment |
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})));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323641%2fhow-to-loop-through-an-object-array-and-output-the-array-with-a-specific-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
- Array.prototype.map()
- Object Destructuring
add a comment |
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:
- Array.prototype.map()
- Object Destructuring
add a comment |
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:
- Array.prototype.map()
- Object Destructuring
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:
- Array.prototype.map()
- 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; }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; }edited Nov 15 '18 at 16:25
answered Nov 15 '18 at 16:20
Mohammad UsmanMohammad Usman
21.2k124558
21.2k124558
add a comment |
add a comment |
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);
Sincedeletemutates original can do it in any loop. No real need to create new array using map() if using mutation approach
– charlietfl
Nov 15 '18 at 16:21
add a comment |
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);
Sincedeletemutates original can do it in any loop. No real need to create new array using map() if using mutation approach
– charlietfl
Nov 15 '18 at 16:21
add a comment |
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);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);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);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);edited Nov 15 '18 at 16:21
answered Nov 15 '18 at 16:20
MohammadMohammad
15.8k123664
15.8k123664
Sincedeletemutates original can do it in any loop. No real need to create new array using map() if using mutation approach
– charlietfl
Nov 15 '18 at 16:21
add a comment |
Sincedeletemutates original can do it in any loop. No real need to create new array using map() if using mutation approach
– charlietfl
Nov 15 '18 at 16:21
Since
delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approach– charlietfl
Nov 15 '18 at 16:21
Since
delete mutates original can do it in any loop. No real need to create new array using map() if using mutation approach– charlietfl
Nov 15 '18 at 16:21
add a comment |
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)add a comment |
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)add a comment |
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)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)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)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 '18 at 16:23
Always SunnyAlways Sunny
16.7k32848
16.7k32848
add a comment |
add a comment |
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})));
add a comment |
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})));
add a comment |
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})));
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 '18 at 16:24
theapologisttheapologist
576215
576215
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323641%2fhow-to-loop-through-an-object-array-and-output-the-array-with-a-specific-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What is your specific property, which property in the array are you looking for?
– squeekyDave
Nov 15 '18 at 16:22