JavaScript: Merge multiple Arrays object in to single Array of objects [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am receiving the following data in the variable
let data = [
[{option: “foo0", value: “bar0”}],
[{option: “foo1", value: “bar1”}],
[{option: “foo2", value: “bar2”}],
[{option: “foo3", value: “bar3”}],
]
and I need to change the response to
[
{option: “foo0", value: “bar0”},
{option: “foo1", value: “bar1”},
{option: “foo2", value: “bar2”},
{option: “foo3", value: “bar3”},
]
how can i do it in JS

Use Array.flat():
const data = [[{"option":"foo0","value":"bar0"}],[{"option":"foo1","value":"bar1"}],[{"option":"foo2","value":"bar2"}],[{"option":"foo3","value":"bar3"}]]
const result = data.flat()
console.log(result)
If Array.flat() is not supported, you can flatten the array by spreading into concat:
const data = [[{"option":"foo0","value":"bar0"}],[{"option":"foo1","value":"bar1"}],[{"option":"foo2","value":"bar2"}],[{"option":"foo3","value":"bar3"}]]
const result = [].concat(...data)
console.log(result)

try this:
let data = [
[{
option: "foo0",
value: "bar0"
}],
[{
option: "foo1",
value: "bar1"
}],
[{
option: "foo2",
value: "bar2"
}],
[{
option: "foo3",
value: "bar3"
}],
];
let result = data.map(v => v[0]);
console.log(result);

Related

Converting Object with nested keys into single object in Javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I want to convert this:
data: {
"3": {
name: ["Missing data for required field."],
},
"5": {
id: ["Same id exist."],
},
}
into this:
data: [{
key: "name",
message: "Missing data for required field.",
row: 3,
},
{
key: "id",
message: "Same id exist.",
row: 5,
},]
how to achieve this with just es6 syntax
You can do this with Object.entries:
const data = {
"3": {
name: ["Missing data for required field."]
},
"5": {
id: ["Same id exist."]
}
};
Object.entries(data).map(([a, b]) => {
return {
key: Object.keys(b)[0],
message: Object.values(b)[0][0],
row: a
};
});
I've made some assumptions:
There will only be one key in each object
The value of the key will be an array with one string

Get array of objects from array of objects with specific properties using rxjs angular [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have database which is array of objects. It look like this:
export const BIKES: Bike[] = [
{
id: 1,
imgUrl:
'https://cyclingmagazine.ca/wp-content/uploads/2018/10/Krypton_A18_2016_red_ultegra_16_1.jpg',
price: 28000,
discount: 71,
main: true,
shop: 'Canada Bike',
name: 'Argon 18',`
},
{
id: 2,
imgUrl:
'https://cyclingmagazine.ca/wp-content/uploads/2018/03/etap.shadowpsd_2048x.png',
price: 7900,
discount: 41,
main: false,
shop: 'Amazon',
name: 'Aquila Cycles',
}
I want to subscribe to array of objects which will consist of 2 properties 'name' and 'shop' and theirs values.
It can be implemented using Array.map
const result = BIKES.map(({name, shop}) => ({ name, shop }));
console.log(result);
or
const result = BIKES.map((item) => {
return {
name: item.name,
shop: item.shop
};
});
console.log(result);

Check if Object has keys [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a Object in this format.
const obj = [{
name: 'name',
items: [{
name: 'name 1',
items: [
{ value: 100 },
{ value: 50 }
]
}]
}]
Imagine that all arrays repeat several times in length and an object inside another.
How I can iterate all objects in all arrays and check if all have the property "name" and "items", if it doesn't have check if has the property "value".
I trying to make a recursion but I don't no how to check if the last "items" has a value to stop and continue to check the rest.
Thanks everyone.
You can use Object.prototype.hasOwnProperty to check if an object has a specific property and Array.prototype.flatMap to map the inner arrays to a single array.
const obj = [{
name: 'name',
items: [{
name: 'name 1',
items: [
{ value: 100 },
{ value: 50 },
{
name: 'name 2',
items: [{
value: 150
}]
}
]
}]
}];
function getValues(arr) {
return arr.flatMap(entry => {
if (entry.hasOwnProperty('name') && entry.hasOwnProperty('items')) {
return getValues(entry.items);
}
return entry.value;
});
}
console.log(getValues(obj));
I resolve my question with:
function check(objs) {
for (let obj of objs) {
if(!('name' in obj) &&
!('value' in obj))
throw new Error('invalid')
if('name' in obj)
check(obj.items)
}
}

Counting object in side array of array object by property javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have data like:
var data = [
{
items: [
{
id: 123
},
{
id: 234
},
{
id: 123
}
]
}, {
items: [
{
id: 123
},
{
id: 234
}
]
}
]
so, I want count object deep in array inside of all data by property 'id'.
ex: data.countObject('id',123) //return 3.
and my data have about xx.000 item, which solution best?
Thanks for help (sorry for my English)
You can use reduce & forEach. Inside the reduce callback you can access the items array using curr.items where acc & curr are just parameters of the call back function. Then you can use curr.items.forEach to get each object inside items array
var data = [{
items: [{
id: 123
},
{
id: 234
},
{
id: 123
}
]
}, {
items: [{
id: 123
},
{
id: 234
}
]
}];
function getCount(id) {
return data.reduce(function(acc, curr) {
// iterate through item array and check if the id is same as
// required id. If same then add 1 to the accumulator
curr.items.forEach(function(item) {
item.id === id ? acc += 1 : acc += 0;
})
return acc;
}, 0) // 0 is the accumulator, initial value is 0
}
console.log(getCount(123))

Convert plain array to Array of objects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array like this
let data = ['String1', 'String2', 'String3', 'String4']
I need to convert this array to an array of objects like so:
data = [
{value: 0, label: 'String1'},
{value: 1, label: 'String2'},
{value: 2, label: 'String3'},
{value: 3, label: 'String4'}
]
How to achieve this most elegantly?
Use map():
const arr = ['String1', 'String2', 'String3', 'String4'];
const res = arr.map((label, value) => ({ value, label }));
console.log(res);

Categories