foreach array with one key [duplicate] - javascript

This question already has answers here:
How to convert an array of objects to object with key value pairs
(6 answers)
Closed 1 year ago.
I've got an object with different values .
let arr = [{"Personal": "1000"},{"sport": "2100"},{"Industrial": "1200"},{"Commercial": "2300"},
{"Fashion": "1300"},{"Documentary": "2600"}]
How can I foreach them and then put them in an object and pick a name for them like this :
"photo_type": {
"Personal": "1000",
"sport": "2100",
"Industrial": "1200",
"Commercial": "2300",
"Fashion": "1300",
"Documentary": "2600"
}
I dont want them to be like 0 and 1 and 2.

You could create an object with Object.assign and by spreading the array.
let array = [{ Personal: "1000" }, { sport: "2100" }, { Industrial: "1200" }, { Commercial: "2300" }, { Fashion: "1300" }, { Documentary: "2600" }],
object = Object.assign({}, ...array);
console.log(object);

You can try this
const arr = [{"Personal": "1000"},{"sport": "2100"},{"Industrial": "1200"},{"Commercial": "2300"},{"Fashion": "1300"},{"Documentary": "2600"}]
let result = {}
arr.forEach(member => {result = {...result, ...member}}

Related

JavsScript: Reduce array of dictionary into Array [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 1 year ago.
I get this response from REST API.
"items": [
{
"name": "one"
},
{
"name": "two"
}
]
I want to reduce it to array ["one", "two"]. Can someone please tell me how?
const res = {
items: [{
name: 'one'
},
{
name: 'two'
}
]
};
console.log(res.items.map(res => res.name));
try this
using map
var items = [
{
name: "one",
},
{
name: "two",
},
];
console.log(items.map((item) => item.name));

Array of flat objects, extract values of a all properties into single object of arrays [duplicate]

This question already has answers here:
best way to convert object with arrays to array with objects and viceversa
(4 answers)
Convert array of objects to object of arrays using lodash
(2 answers)
Closed 2 years ago.
I would like to go from this:
[{
"uuid": 'oaiwngoiasofoi328',
"property1": 3,
"property2": 3,
...many
},
...many ]
to this:
{
"uuid": ['oaiwngoiasofoi328','829rh83hr9h9h29','mejieoainfoi',...],
"property1": [3,3,2,...],
"property2": [3,3,2,...],
...many
}
all the properties will be named the same in each object of the source array
maybe I could use a for loop with map... ??
I've recreated your environment. I think it can help you.
Use Array.reduce() to reduce it to single object.
const data = [
{
uuid: 'adadkljalkd',
'property1': 3,
'property2': 4,
},
{
uuid: 'alkisdfj',
'property1': 4,
'property2': 5,
},
{
uuid: 'klilkkll',
'property1': 7,
'property2': 8,
}
];
const res = data.reduce((acc, obj) => {
Object.keys(obj).forEach(key => {
acc[key] = acc[key] || [];
acc[key].push(obj[key]);
})
return acc;
}, {});
console.log(res);

return statement does not work in forEach loop [duplicate]

This question already has answers here:
What does `return` keyword mean inside `forEach` function? [duplicate]
(2 answers)
How can I get the index of an object by its property in JavaScript?
(22 answers)
Closed 3 years ago.
I'm trying to write a function returnIndex that returns the index of an object in an array, e.g. myArr = [{ "name": "Bob", "age": 4 }, { "name": "Kirk", "age": 11 }], by passing in a given key/value pair: e.g. returnIndex(myArr, "name", "Bob") should return 0.
I tried this:
const myArr = [{ "name": "Bob", "age": 4 }, { "name": "Kirk", "age": 11 }]
const returnIndex = (arr, key, value) => {
arr.forEach((item, index) => {
if (item[key] === value) { return index }
})
}
const i = returnIndex(myArr, "name", "Bob")
console.log(i)
I get undefined i in the console.
But when I create a throw-away variable i:
const myArr = [{ "name": "Bob", "age": 4 }, { "name": "Kirk", "age": 11 }]
const returnIndex = (arr, key, value) => {
let i
arr.forEach((item, index) => {
if (item[key] === value) { i = index }
})
return i
}
const i = returnIndex(myArr, "name", "Bob")
console.log(i)
Then it works. Why is it that I cannot return index directly but have to set up a throw-away variable i? I don't understand this -- Help please!
Also if you have a better way to solve this problem, please do share. Cheers!
You could take Array#findIndex, which returns on the first find with the index.
const
returnIndex = (arr, key, value) => arr.findIndex(item => item[key] === value),
myArr = [{ "name": "Bob", "age": 4 }, { "name": "Kirk", "age": 11 }],
i = returnIndex(myArr, "name", "Bob")
console.log(i);

How to create a JSON array from below JSON with key-value? [duplicate]

This question already has answers here:
How to transpose a javascript object into a key/value array
(11 answers)
How to convert an object to array of objects [duplicate]
(5 answers)
Closed 3 years ago.
Need to create below output from the JSON obj
let obj = {"id" : 1, "name": "John"};
expected result:
[
{key: "id", value: "1"},
{key: "name", value: "John"}
]
You can get the keys of obj using Object.keys() and map them to objects.
const obj = {
"id": 1,
"name": "John"
};
const result = Object.keys(obj).map(k => ({
key: k,
value: obj[k]
}));
console.log(result);
You can try this:
JSON.stringify(
Object.entries({"id" : 1, "name": "John"}).map(([key, value]) => ({
key: key,
value: value
}))
)

Change array in to object in react js [duplicate]

This question already has answers here:
How do I convert array of Objects into one Object in JavaScript?
(17 answers)
Convert Javascript array of objects into one object
(4 answers)
Closed 3 years ago.
This is my array format
let array = [
0: {
key:"name",
value: "John"
},
1: {
key:"age",
value:25
},
2: {
key:"job",
value:"software engineer"
},...
];
Change this array to object in given below format
{
name: "John",
age: 27,
job: "software engineer"
}
You can achieve it using forEach on the array.
Give this a try:
const array = [{
key: "name",
value: "John"
}, {
key: "age",
value: 25
}, {
key: "job",
value: "software engineer"
}];
const expected = {};
array.forEach(item => expected[item.key] = item.value);
console.log(expected);
You can use Array.prototype.reduce() to do this:
let array = [
{
key:"name",
value: "John"
},
{
key:"age",
value:25
},
{
key:"job",
value:"software engineer"
}
];
let result = array.reduce((a,b) => {
a[b.key] = b.value;
return a;
}, {});
console.log(result);
All you need to do is to loop over the array using forEach, for loop, while loop etcand push the values in a new object.
Also make sure that your array syntax is correct because the way you have mentioned it in the question is incorrect.
const data = [{ key:"name", value: "John" },{ key:"age", value:25 },{ key:"job", value:"software engineer" } ];
const res = {};
data.forEach(item => { res[item.key] = item.value});
console.log(res);

Categories

Resources