Please read carefully the question, this is not a duplicate of:
- ECMAScript 6 arrow function that returns an object
- javascript cannot map array of objects with nested values
- map function for objects (instead of arrays)
- Loop through an array in JavaScript
- map function for objects (instead of arrays)
- Loop through an array in JavaScript
Let's consider the following array of object:
var obj = [{ 'key1' : 'value1' }, { 'key2' : 'value2' }];
I would like to map the array to get keys and values for each object. Something like:
obj.map((key, val) => console.log(key, val));
I already try many stuff like Object.entries(obj)
but it always results in plicated solution with many brackets like Object.entries(obj)[0][1]
Is there a simple, nice and efficient way to map an array of object? Note I need key and value for each object
Please read carefully the question, this is not a duplicate of:
- ECMAScript 6 arrow function that returns an object
- javascript cannot map array of objects with nested values
- map function for objects (instead of arrays)
- Loop through an array in JavaScript
- map function for objects (instead of arrays)
- Loop through an array in JavaScript
Let's consider the following array of object:
var obj = [{ 'key1' : 'value1' }, { 'key2' : 'value2' }];
I would like to map the array to get keys and values for each object. Something like:
obj.map((key, val) => console.log(key, val));
I already try many stuff like Object.entries(obj)
but it always results in plicated solution with many brackets like Object.entries(obj)[0][1]
Is there a simple, nice and efficient way to map an array of object? Note I need key and value for each object
Share Improve this question edited Jan 25, 2020 at 16:00 Uwe Keim 40.8k61 gold badges190 silver badges304 bronze badges asked Jan 25, 2020 at 15:14 FifiFifi 3,6153 gold badges31 silver badges62 bronze badges 13-
objs.flatMap(Object.entries)
should do? – Bergi Commented Jan 25, 2020 at 15:19 -
What is the expected output value, to what do you want to map the array of objects? Your "something like" only does log the keys and values, which is trivial and does not need
map
. What exactly do you need? – Bergi Commented Jan 25, 2020 at 15:20 - I want to create a HTTP request like key1=value1&key2=value2, but I already know how to do that, I just have difficulties accessing the object keys and values. – Fifi Commented Jan 25, 2020 at 15:23
- Please show your code then. It seems you have no problem with iterating through the array, but just getting key and value from the object? The solution to that would be to use a less weird input format - why an array of single-property objects instead of one object with multiple properties that you could simply enumerate? – Bergi Commented Jan 25, 2020 at 15:28
- 1 @Fifi what final result are you looking for? Is your expected result an array, or a single obj, and what values are in it? – nonopolarity Commented Jan 25, 2020 at 15:31
3 Answers
Reset to default 4You seem like you only want to print it out or access them:
.map
changes an array to a different array, which doesn't seem like what you are looking for.
var objs = [{ 'key1' : 'value1' }, { 'key2' : 'value2' }];
objs.forEach(obj => {
for (let p in obj) console.log(p, obj[p]);
});
If you are looking for key1=value1&key2=value2
as the answer and you know you only have 1 key and value in each object, then it is:
let objs = [{ 'key1' : 'value1' }, { 'key2' : 'value2' }];
let s = objs.map(obj => `${Object.keys(obj)[0]}=${Object.values(obj)[0]}`).join("&");
console.log(s);
But you probably want to use encodeURIComponent()
to encode the params, making it:
let objs = [{ 'key1' : 'value1 hello' }, { 'key2' : 'value2 & 3' }];
let s = objs.map(obj => `${encodeURIComponent(Object.keys(obj)[0])}=${(encodeURIComponent(Object.values(obj)[0]))}`).join("&");
console.log(s);
If your keys are all alphanumeric and underscore characters, then you shouldn't need to use encodeURIComponent()
on the key.
var obj = [{ 'key1' : 'value1' }, { 'key2' : 'value2' }];
obj.forEach(el => {
for (var prop in el) {
console.log(prop, el[prop])
}
})
// results:
// key1 value1
// key2 value2
Not as clean as what @nopole answer, but this kind achieve what you want for a key, value object.
var objs = [{ 'key1' : 'value1' }, { 'key2' : 'value2' }];
objs.forEach(obj => {
// loop over keys-and-values
for (let [key, value] of Object.entries(obj)) {
console.log(key, value);
}
});
Also this works for object with more than one key:
var objs = [{ 'key1' : 'value1', "key2":"value2" }, { 'key3' : 'value3' }];
objs.forEach(obj => {
// loop over keys-and-values
for (let [key, value] of Object.entries(obj)) {
console.log(key, value);
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744158006a4560954.html
评论列表(0条)