This question already has answers here:
How to merge each object within arrays by index?
(1 answer)
Combine arrays of objects by object index [duplicate]
(4 answers)
Closed 1 year ago.
I want to merge the objects that are in the same index on two different arrays of objects.
Which is the most simple and easy-to-understand way to go about this?
Here is the first array
const countries = [
{
"name": "Sweden",
"nativeName": "Sverige"
},
{
"name": "Norway",
"nativeName": "Norge"
},
{
"name": "Iceland",
"nativeName": "Ísland"
}
]
Here is the second array
const countryCodes = [
{
"country_id": "SE",
},
{
"country_id": "NO",
},
{
"country_id": "IS",
}
]
I want to end up with this.
const countriesAndCodes = [
{
"name": "Sweden",
"country_id": "SE",
"nativeName": "Sverige"
},
{
"name": "Norway",
"country_id": "NO",
"nativeName": "Norge"
},
{
"name": "Iceland",
"country_id": "IS",
"nativeName": "Ísland"
}
]
One option:
countries
.map((country, i) => {
const countryCode = countryCodes[i];
const mergedCountry = ...; // Whatever technique to merge the two objects
return mergedCountry;
})
Though, I would probably look to use a zip function on a library like Lodash
Related
This question already has answers here:
Get the property of the difference between two objects in javascript
(3 answers)
Closed last month.
So here i have two object data:
{
"obj1": {
"product": "Book",
"category": "sci-fi",
"title": "interstellar",
},
"obj2": {
"product": "Book",
"category": "horror",
"title": "evil dead",
},
"differences": []
}
From that data, i need to comparing each value from obj1 and obj2 variables find keys that have difference values from these two objects then pushing it into differences variable.
Expected Result:
{
"obj1": {
"product": "Book",
"category": "sci-fi",
"title": "interstellar",
},
"obj2": {
"product": "Book",
"category": "horror",
"title": "evil dead",
},
"differences": [
"category",
"title"
]
}
Does anyone have recommendation to solving it?
You can simply make 2 loop, one for each object and then compare by key :
let data = {
"obj1": {
"product": "Book",
"category": "sci-fi",
"title": "interstellar",
},
"obj2": {
"product": "Book",
"category": "horror",
"title": "evil dead",
},
"differences": []
};
for (let key1 in data.obj1) {
for (let key2 in data.obj2) {
if (key1 === key2 && data.obj1[key1] !== data.obj2[key2]) {
data.differences.push(key1);
}
}
}
console.log(data);
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
From an array of objects, extract value of a property as array
(24 answers)
Loop through an array in JavaScript
(46 answers)
Closed 2 years ago.
So I'm building some content with Formio and it has the ability to add custom JSON content for the drop down options.
This is my JSON:
[
{
"MBB": [
{
"Name": "BYO Sim"
},
{
"Name": "Device Sim"
},
{
"Name": "Apple Watch"
},
{
"Name": "Samsung Watch"
}
],
"WB": [
{
"Name": "4G with Device"
},
{
"Name": "4G without Device"
},
{
"Name": "5G"
}
]
}
]
What I want to do is show all values that are under MBB:Name
I have found that when I enter the below, I get the value "Device Sim" returned but I want to return all values under MBB and Name as I will soon be adding more options, not just Name.
item.MBB[1].Name
If I just do:
item.MBB.Name
I just get an undefined outcome so I'm a little lost.
Any help would be appreciated!
You need a function to extract an array of just Name from the array of objects, each of which contains its own Name.
item.MBB.map(obj => obj.Name)
You can remove the first array:
{
"MBB": [
{
"Name": "BYO Sim"
},
{
"Name": "Device Sim"
},
{
"Name": "Apple Watch"
},
{
"Name": "Samsung Watch"
}
],
"WB": [
{
"Name": "4G with Device"
},
{
"Name": "4G without Device"
},
{
"Name": "5G"
}
]
}
Use map method of the array for the iteration.
const data =
{
"MBB": [
{
"Name": "BYO Sim"
},
{
"Name": "Device Sim"
},
{
"Name": "Apple Watch"
},
{
"Name": "Samsung Watch"
}
],
"WB": [
{
"Name": "4G with Device"
},
{
"Name": "4G without Device"
},
{
"Name": "5G"
}
]
}
;
const result = data['MBB'].map(({Name}) => Name);
console.log(result);
This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 2 years ago.
I need to select multiple array elements that have the same value.
Using array.find () returns only the first element that satisfies the query condition.
The construction below shows only "Donald Trump" in console:
const data = [
{
"position": "president",
"name": "Donald Trump",
"language": "english"
},
{
"position": "president",
"name": "Vladimir Putin",
"language": "russian"
},
{
"position": "king",
"name": "Shutruk-Nahhunte",
"language": "elamite"
},
];
let result = data.find(elem => elem.position == "president");
console.log(result.name);
But I need to get all the values as an array, - something like this:
[
"Donald Trump",
"Vladimir Putin"
]
How to do it right, considering also that the real array is huge.
Thanks for any help!
I use filter to do this task
const data = [
{
"position": "president",
"name": "Donald Trump",
"language": "english"
},
{
"position": "president",
"name": "Vladimir Putin",
"language": "russian"
},
{
"position": "king",
"name": "Shutruk-Nahhunte",
"language": "elamite"
},
];
const newArray= data.filter(x=>x.position==='president')
let nameArray=newArray.map(x=>x.name)
console.log(nameArray)
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
How to get values from JS object
(2 answers)
Closed 3 years ago.
I'm trying to get data out of a JSON using a value.
I have an array of objects:
"fruits": [
{
"Name": "Orange",
"Quantity": "5"
},
{
"Name": "Apple",
"Quantity": "2"
}
]
And I also have the name "Apple". How can I get the quantity of Apples?
I'm stuck on this..
Thanks!
fruits.find(fruit => fruit.Name==="Apple").Quantity is what you are looking for:
const fruits = [
{
"Name": "Orange",
"Quantity": "5"
},
{
"Name": "Apple",
"Quantity": "2"
}
]
console.log(fruits.find(fruit => fruit.Name==="Apple").Quantity);
Using a forEach you can check each element and if the Name matches get the Quantity
var fruits = [{
"Name": "Orange",
"Quantity": "5"
},
{
"Name": "Apple",
"Quantity": "2"
}
]
fruits.forEach(function(element) {
if (element.Name === 'Apple') {
console.log(element.Quantity)
}
});
This question already has answers here:
Filter array of objects by multiple properties and values
(4 answers)
Closed 4 years ago.
Suppose I have an array like:
const items=[{
"taskType": "type2",
"taskName": "two",
"id": "19d0da63-dfd0-4c00-a13a-cc822fc81298"
},
{
"taskType": "type1",
"taskName": "two",
"id": "c5385595-2104-409d-a676-c1b57346f63e"
}]
I want to have an arrow (filter) function that returns all items except for where taskType=type2 and taskName=two. So in this case it just returns the second item?
You can try negating the condition in Array.prototype.filter()
const items=[{
"taskType": "type2",
"taskName": "two",
"id": "19d0da63-dfd0-4c00-a13a-cc822fc81298"
},
{
"taskType": "type1",
"taskName": "two",
"id": "c5385595-2104-409d-a676-c1b57346f63e"
}]
var res = items.filter(task => !(task.taskType == 'type2' && task.taskName == 'two'));
console.log(res);
You can use lodash's _.reject(). Use an object as a predicate, and define the properties and values to reject by:
const items= [{
"taskType": "type2",
"taskName": "two",
"id": "19d0da63-dfd0-4c00-a13a-cc822fc81298"
},
{
"taskType": "type3",
"taskName": "two",
"id": "19d0da63-dfd0-4c00-a13a-cc822fc81298"
},
{
"taskType": "type1",
"taskName": "two",
"id": "c5385595-2104-409d-a676-c1b57346f63e"
}]
const result = _.reject(items, { taskType: "type2", taskName: "two" });
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>