How can I get first value only in each JSON object
x variable has a JSON value like below
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
the output should be
y = [7,1];
How can I get the JSON multiple object first value only? Thanks.
javascript arrays json object
add a comment |
x variable has a JSON value like below
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
the output should be
y = [7,1];
How can I get the JSON multiple object first value only? Thanks.
javascript arrays json object
This question has nothing to do with JSON.
– Brad
Nov 13 '18 at 3:01
add a comment |
x variable has a JSON value like below
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
the output should be
y = [7,1];
How can I get the JSON multiple object first value only? Thanks.
javascript arrays json object
x variable has a JSON value like below
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
the output should be
y = [7,1];
How can I get the JSON multiple object first value only? Thanks.
javascript arrays json object
javascript arrays json object
edited Nov 13 '18 at 3:22
hev1
5,5683527
5,5683527
asked Nov 13 '18 at 2:52
imjayabalimjayabal
308
308
This question has nothing to do with JSON.
– Brad
Nov 13 '18 at 3:01
add a comment |
This question has nothing to do with JSON.
– Brad
Nov 13 '18 at 3:01
This question has nothing to do with JSON.
– Brad
Nov 13 '18 at 3:01
This question has nothing to do with JSON.
– Brad
Nov 13 '18 at 3:01
add a comment |
4 Answers
4
active
oldest
votes
Object properties are only reliably ordered in semi-modern browsers - your desired output may not be reliably determinable from code on running older browsers (unless you have a set order of the property names in an array, or unless the properties are numeric, or something like that). But, on newer browsers, you can .map
the input array and extract the first value found by Object.values
from the object you're iterating over:
const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
const result = x.map((obj) => Object.values(obj)[0]);
console.log(result);
Also note that, as always, there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'
. If there are no strings, serialization, or deserialization involved, then JSON is not involved either.
If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);
– vladimir.shein
Nov 13 '18 at 3:11
Opps ..missed that
– charlietfl
Nov 13 '18 at 3:13
What about thisconst result = x.map((obj) => obj.a);
can I use like this?
– imjayabal
Nov 13 '18 at 5:01
@imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need forObject.values
in that case.
– CertainPerformance
Nov 13 '18 at 5:02
add a comment |
If you're sure about the key you want to access, then you just need to iterate from x and then push the result on your y.
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
let y = ;
for(let i in x) {
y.push(x[i].a);
}
console.log(y);
add a comment |
You could combine Array#map
with Object.values()
to achieve this; the mapping would transform each item of your input array x
to the resulting array y
. For each item in the resulting array y
, you would select the first value of the corresponding item from x
.
To select the first value of an item in x
, that item is passed to Object.values()
. The result of Object.values()
is an array of the values of that item, and it's by this that you can access and "map" the first value of each item in y
:
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
let y = x.map(item => Object.values(item)[0]);
console.log(y)
add a comment |
var data = ;
x.forEach((elem) => {
data.push(elem.a);
});
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%2f53273090%2fhow-can-i-get-first-value-only-in-each-json-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
Object properties are only reliably ordered in semi-modern browsers - your desired output may not be reliably determinable from code on running older browsers (unless you have a set order of the property names in an array, or unless the properties are numeric, or something like that). But, on newer browsers, you can .map
the input array and extract the first value found by Object.values
from the object you're iterating over:
const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
const result = x.map((obj) => Object.values(obj)[0]);
console.log(result);
Also note that, as always, there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'
. If there are no strings, serialization, or deserialization involved, then JSON is not involved either.
If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);
– vladimir.shein
Nov 13 '18 at 3:11
Opps ..missed that
– charlietfl
Nov 13 '18 at 3:13
What about thisconst result = x.map((obj) => obj.a);
can I use like this?
– imjayabal
Nov 13 '18 at 5:01
@imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need forObject.values
in that case.
– CertainPerformance
Nov 13 '18 at 5:02
add a comment |
Object properties are only reliably ordered in semi-modern browsers - your desired output may not be reliably determinable from code on running older browsers (unless you have a set order of the property names in an array, or unless the properties are numeric, or something like that). But, on newer browsers, you can .map
the input array and extract the first value found by Object.values
from the object you're iterating over:
const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
const result = x.map((obj) => Object.values(obj)[0]);
console.log(result);
Also note that, as always, there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'
. If there are no strings, serialization, or deserialization involved, then JSON is not involved either.
If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);
– vladimir.shein
Nov 13 '18 at 3:11
Opps ..missed that
– charlietfl
Nov 13 '18 at 3:13
What about thisconst result = x.map((obj) => obj.a);
can I use like this?
– imjayabal
Nov 13 '18 at 5:01
@imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need forObject.values
in that case.
– CertainPerformance
Nov 13 '18 at 5:02
add a comment |
Object properties are only reliably ordered in semi-modern browsers - your desired output may not be reliably determinable from code on running older browsers (unless you have a set order of the property names in an array, or unless the properties are numeric, or something like that). But, on newer browsers, you can .map
the input array and extract the first value found by Object.values
from the object you're iterating over:
const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
const result = x.map((obj) => Object.values(obj)[0]);
console.log(result);
Also note that, as always, there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'
. If there are no strings, serialization, or deserialization involved, then JSON is not involved either.
Object properties are only reliably ordered in semi-modern browsers - your desired output may not be reliably determinable from code on running older browsers (unless you have a set order of the property names in an array, or unless the properties are numeric, or something like that). But, on newer browsers, you can .map
the input array and extract the first value found by Object.values
from the object you're iterating over:
const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
const result = x.map((obj) => Object.values(obj)[0]);
console.log(result);
Also note that, as always, there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'
. If there are no strings, serialization, or deserialization involved, then JSON is not involved either.
const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
const result = x.map((obj) => Object.values(obj)[0]);
console.log(result);
const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
const result = x.map((obj) => Object.values(obj)[0]);
console.log(result);
answered Nov 13 '18 at 2:56
CertainPerformanceCertainPerformance
79.5k143865
79.5k143865
If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);
– vladimir.shein
Nov 13 '18 at 3:11
Opps ..missed that
– charlietfl
Nov 13 '18 at 3:13
What about thisconst result = x.map((obj) => obj.a);
can I use like this?
– imjayabal
Nov 13 '18 at 5:01
@imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need forObject.values
in that case.
– CertainPerformance
Nov 13 '18 at 5:02
add a comment |
If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);
– vladimir.shein
Nov 13 '18 at 3:11
Opps ..missed that
– charlietfl
Nov 13 '18 at 3:13
What about thisconst result = x.map((obj) => obj.a);
can I use like this?
– imjayabal
Nov 13 '18 at 5:01
@imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need forObject.values
in that case.
– CertainPerformance
Nov 13 '18 at 5:02
If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);
– vladimir.shein
Nov 13 '18 at 3:11
If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);
– vladimir.shein
Nov 13 '18 at 3:11
Opps ..missed that
– charlietfl
Nov 13 '18 at 3:13
Opps ..missed that
– charlietfl
Nov 13 '18 at 3:13
What about this
const result = x.map((obj) => obj.a);
can I use like this?– imjayabal
Nov 13 '18 at 5:01
What about this
const result = x.map((obj) => obj.a);
can I use like this?– imjayabal
Nov 13 '18 at 5:01
@imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need for
Object.values
in that case.– CertainPerformance
Nov 13 '18 at 5:02
@imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need for
Object.values
in that case.– CertainPerformance
Nov 13 '18 at 5:02
add a comment |
If you're sure about the key you want to access, then you just need to iterate from x and then push the result on your y.
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
let y = ;
for(let i in x) {
y.push(x[i].a);
}
console.log(y);
add a comment |
If you're sure about the key you want to access, then you just need to iterate from x and then push the result on your y.
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
let y = ;
for(let i in x) {
y.push(x[i].a);
}
console.log(y);
add a comment |
If you're sure about the key you want to access, then you just need to iterate from x and then push the result on your y.
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
let y = ;
for(let i in x) {
y.push(x[i].a);
}
console.log(y);
If you're sure about the key you want to access, then you just need to iterate from x and then push the result on your y.
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
let y = ;
for(let i in x) {
y.push(x[i].a);
}
console.log(y);
answered Nov 13 '18 at 3:19
Match SumayaMatch Sumaya
112
112
add a comment |
add a comment |
You could combine Array#map
with Object.values()
to achieve this; the mapping would transform each item of your input array x
to the resulting array y
. For each item in the resulting array y
, you would select the first value of the corresponding item from x
.
To select the first value of an item in x
, that item is passed to Object.values()
. The result of Object.values()
is an array of the values of that item, and it's by this that you can access and "map" the first value of each item in y
:
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
let y = x.map(item => Object.values(item)[0]);
console.log(y)
add a comment |
You could combine Array#map
with Object.values()
to achieve this; the mapping would transform each item of your input array x
to the resulting array y
. For each item in the resulting array y
, you would select the first value of the corresponding item from x
.
To select the first value of an item in x
, that item is passed to Object.values()
. The result of Object.values()
is an array of the values of that item, and it's by this that you can access and "map" the first value of each item in y
:
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
let y = x.map(item => Object.values(item)[0]);
console.log(y)
add a comment |
You could combine Array#map
with Object.values()
to achieve this; the mapping would transform each item of your input array x
to the resulting array y
. For each item in the resulting array y
, you would select the first value of the corresponding item from x
.
To select the first value of an item in x
, that item is passed to Object.values()
. The result of Object.values()
is an array of the values of that item, and it's by this that you can access and "map" the first value of each item in y
:
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
let y = x.map(item => Object.values(item)[0]);
console.log(y)
You could combine Array#map
with Object.values()
to achieve this; the mapping would transform each item of your input array x
to the resulting array y
. For each item in the resulting array y
, you would select the first value of the corresponding item from x
.
To select the first value of an item in x
, that item is passed to Object.values()
. The result of Object.values()
is an array of the values of that item, and it's by this that you can access and "map" the first value of each item in y
:
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
let y = x.map(item => Object.values(item)[0]);
console.log(y)
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
let y = x.map(item => Object.values(item)[0]);
console.log(y)
let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]
let y = x.map(item => Object.values(item)[0]);
console.log(y)
answered Nov 13 '18 at 2:56
Dacre DennyDacre Denny
10.9k4929
10.9k4929
add a comment |
add a comment |
var data = ;
x.forEach((elem) => {
data.push(elem.a);
});
add a comment |
var data = ;
x.forEach((elem) => {
data.push(elem.a);
});
add a comment |
var data = ;
x.forEach((elem) => {
data.push(elem.a);
});
var data = ;
x.forEach((elem) => {
data.push(elem.a);
});
edited Nov 13 '18 at 8:20
dferenc
4,531122131
4,531122131
answered Nov 13 '18 at 5:53
Ashish KirodianAshish Kirodian
765
765
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%2f53273090%2fhow-can-i-get-first-value-only-in-each-json-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
This question has nothing to do with JSON.
– Brad
Nov 13 '18 at 3:01