how to push all objects keys to a separate array in array using angular or javascript?
I have array of objects like this
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
and I want an array like this
[
['Jack',23],
['Robin',25]
]
I tried this code
var myArr = ;
var input = [
{name : 'Jack', age : 23},
{name : 'Robin', age : 25}
];
input.forEach((item,index)=>{
for (var k in item) {
myArr.push(item[k]);
}
})
But it is producing result like this
["Jack", 23, "Robin", 25]
javascript arrays object angular5 javascript-objects
add a comment |
I have array of objects like this
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
and I want an array like this
[
['Jack',23],
['Robin',25]
]
I tried this code
var myArr = ;
var input = [
{name : 'Jack', age : 23},
{name : 'Robin', age : 25}
];
input.forEach((item,index)=>{
for (var k in item) {
myArr.push(item[k]);
}
})
But it is producing result like this
["Jack", 23, "Robin", 25]
javascript arrays object angular5 javascript-objects
add a comment |
I have array of objects like this
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
and I want an array like this
[
['Jack',23],
['Robin',25]
]
I tried this code
var myArr = ;
var input = [
{name : 'Jack', age : 23},
{name : 'Robin', age : 25}
];
input.forEach((item,index)=>{
for (var k in item) {
myArr.push(item[k]);
}
})
But it is producing result like this
["Jack", 23, "Robin", 25]
javascript arrays object angular5 javascript-objects
I have array of objects like this
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
and I want an array like this
[
['Jack',23],
['Robin',25]
]
I tried this code
var myArr = ;
var input = [
{name : 'Jack', age : 23},
{name : 'Robin', age : 25}
];
input.forEach((item,index)=>{
for (var k in item) {
myArr.push(item[k]);
}
})
But it is producing result like this
["Jack", 23, "Robin", 25]
javascript arrays object angular5 javascript-objects
javascript arrays object angular5 javascript-objects
edited Nov 23 '18 at 7:41
Mohammad Usman
21.4k124658
21.4k124658
asked Nov 23 '18 at 7:21
Ahsan Alam SiddiquiAhsan Alam Siddiqui
219
219
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Simply pass Object.values
as callback to .map()
:
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let result = array.map(Object.values);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
2
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
– HMR
Nov 23 '18 at 7:51
add a comment |
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
arrayOfValues = array.map(({name, age})=> ([name, age]))
console.log(arrayOfValues)
@HMR Thank you very much for sharing the valuable information.
The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.
More Information
2
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this is the only valid answer.
– HMR
Nov 23 '18 at 7:49
You're Correct @HMR
– Nitish Narang
Nov 23 '18 at 8:20
1
Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.
– Nitish Narang
Nov 23 '18 at 8:22
add a comment |
This would be Array.map
and Object.values
like below
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let res = array.map(d => Object.values(d))
console.log(res)
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
– HMR
Nov 23 '18 at 7:50
Oh is it!!! Thanks for the info @HMR :)
– Nitish Narang
Nov 23 '18 at 8:17
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%2f53442234%2fhow-to-push-all-objects-keys-to-a-separate-array-in-array-using-angular-or-javas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simply pass Object.values
as callback to .map()
:
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let result = array.map(Object.values);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
2
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
– HMR
Nov 23 '18 at 7:51
add a comment |
Simply pass Object.values
as callback to .map()
:
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let result = array.map(Object.values);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
2
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
– HMR
Nov 23 '18 at 7:51
add a comment |
Simply pass Object.values
as callback to .map()
:
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let result = array.map(Object.values);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Simply pass Object.values
as callback to .map()
:
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let result = array.map(Object.values);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let result = array.map(Object.values);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let result = array.map(Object.values);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered Nov 23 '18 at 7:24
Mohammad UsmanMohammad Usman
21.4k124658
21.4k124658
2
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
– HMR
Nov 23 '18 at 7:51
add a comment |
2
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
– HMR
Nov 23 '18 at 7:51
2
2
The order in which you get the values in Object.values or Object.keys or
for ... in
or for ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23}
specifically to ['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.– HMR
Nov 23 '18 at 7:51
The order in which you get the values in Object.values or Object.keys or
for ... in
or for ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23}
specifically to ['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.– HMR
Nov 23 '18 at 7:51
add a comment |
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
arrayOfValues = array.map(({name, age})=> ([name, age]))
console.log(arrayOfValues)
@HMR Thank you very much for sharing the valuable information.
The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.
More Information
2
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this is the only valid answer.
– HMR
Nov 23 '18 at 7:49
You're Correct @HMR
– Nitish Narang
Nov 23 '18 at 8:20
1
Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.
– Nitish Narang
Nov 23 '18 at 8:22
add a comment |
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
arrayOfValues = array.map(({name, age})=> ([name, age]))
console.log(arrayOfValues)
@HMR Thank you very much for sharing the valuable information.
The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.
More Information
2
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this is the only valid answer.
– HMR
Nov 23 '18 at 7:49
You're Correct @HMR
– Nitish Narang
Nov 23 '18 at 8:20
1
Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.
– Nitish Narang
Nov 23 '18 at 8:22
add a comment |
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
arrayOfValues = array.map(({name, age})=> ([name, age]))
console.log(arrayOfValues)
@HMR Thank you very much for sharing the valuable information.
The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.
More Information
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
arrayOfValues = array.map(({name, age})=> ([name, age]))
console.log(arrayOfValues)
@HMR Thank you very much for sharing the valuable information.
The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.
More Information
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
arrayOfValues = array.map(({name, age})=> ([name, age]))
console.log(arrayOfValues)
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
arrayOfValues = array.map(({name, age})=> ([name, age]))
console.log(arrayOfValues)
edited Nov 23 '18 at 14:30
answered Nov 23 '18 at 7:27
Mohammed AshfaqMohammed Ashfaq
1,7712713
1,7712713
2
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this is the only valid answer.
– HMR
Nov 23 '18 at 7:49
You're Correct @HMR
– Nitish Narang
Nov 23 '18 at 8:20
1
Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.
– Nitish Narang
Nov 23 '18 at 8:22
add a comment |
2
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this is the only valid answer.
– HMR
Nov 23 '18 at 7:49
You're Correct @HMR
– Nitish Narang
Nov 23 '18 at 8:20
1
Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.
– Nitish Narang
Nov 23 '18 at 8:22
2
2
The order in which you get the values in Object.values or Object.keys or
for ... in
or for ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23}
specifically to ['Jack',23]
then this is the only valid answer.– HMR
Nov 23 '18 at 7:49
The order in which you get the values in Object.values or Object.keys or
for ... in
or for ... of
from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23}
specifically to ['Jack',23]
then this is the only valid answer.– HMR
Nov 23 '18 at 7:49
You're Correct @HMR
– Nitish Narang
Nov 23 '18 at 8:20
You're Correct @HMR
– Nitish Narang
Nov 23 '18 at 8:20
1
1
Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.
– Nitish Narang
Nov 23 '18 at 8:22
Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.
– Nitish Narang
Nov 23 '18 at 8:22
add a comment |
This would be Array.map
and Object.values
like below
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let res = array.map(d => Object.values(d))
console.log(res)
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
– HMR
Nov 23 '18 at 7:50
Oh is it!!! Thanks for the info @HMR :)
– Nitish Narang
Nov 23 '18 at 8:17
add a comment |
This would be Array.map
and Object.values
like below
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let res = array.map(d => Object.values(d))
console.log(res)
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
– HMR
Nov 23 '18 at 7:50
Oh is it!!! Thanks for the info @HMR :)
– Nitish Narang
Nov 23 '18 at 8:17
add a comment |
This would be Array.map
and Object.values
like below
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let res = array.map(d => Object.values(d))
console.log(res)
This would be Array.map
and Object.values
like below
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let res = array.map(d => Object.values(d))
console.log(res)
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let res = array.map(d => Object.values(d))
console.log(res)
let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];
let res = array.map(d => Object.values(d))
console.log(res)
answered Nov 23 '18 at 7:22
Nitish NarangNitish Narang
2,9701815
2,9701815
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
– HMR
Nov 23 '18 at 7:50
Oh is it!!! Thanks for the info @HMR :)
– Nitish Narang
Nov 23 '18 at 8:17
add a comment |
The order in which you get the values in Object.values or Object.keys orfor ... in
orfor ... of
from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert{'name' : 'Jack', 'age':23}
specifically to['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
– HMR
Nov 23 '18 at 7:50
Oh is it!!! Thanks for the info @HMR :)
– Nitish Narang
Nov 23 '18 at 8:17
The order in which you get the values in Object.values or Object.keys or
for ... in
or for ... of
from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23}
specifically to ['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.– HMR
Nov 23 '18 at 7:50
The order in which you get the values in Object.values or Object.keys or
for ... in
or for ... of
from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23}
specifically to ['Jack',23]
then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.– HMR
Nov 23 '18 at 7:50
Oh is it!!! Thanks for the info @HMR :)
– Nitish Narang
Nov 23 '18 at 8:17
Oh is it!!! Thanks for the info @HMR :)
– Nitish Narang
Nov 23 '18 at 8:17
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%2f53442234%2fhow-to-push-all-objects-keys-to-a-separate-array-in-array-using-angular-or-javas%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