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 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
This question already has an answer here:
How to create a multi-filter function to filter out multiple attributes?
(1 answer)
Closed 2 years ago.
I have this array of objects
const bnb = [
{
"id": "id1",
"img": ["tres", "dos", "uno"],
"title": "one",
"city": "texas",
"saved": false,
"tags": ["bath"]
},
{
"id": "id2",
"img": ["quatro", "uno", "tres"],
"title": "two",
"city": "denver",
"saved": false,
"tags": ["wc"]
},
{
"id": "id3",
"img": ["uno", "dos", "tres"],
"title": "three",
"city": "vancouver",
"saved": false,
"tags": ["wc", "bath"]
},
{
"id": "id4",
"img": ["dos", "uno", "tres"],
"title": "four",
"city": "berlin",
"saved": false,
"tags": ["bath", "parking"]
},
{
"id": "id5",
"img": ["tres", "uno", "quatro"],
"title": "five",
"city": "paris",
"saved": false,
"tags": ["loft", "parking", "kitchen"]
},
{
"id": "id6",
"img": ["quatro", "uno", "tres"],
"title": "six",
"city": "barcelona",
"saved": false,
"tags": ["bath", "wc"]
},
{
"id": "id7",
"img": ["uno", "tres", "quatro"],
"title": "seven",
"city": "seul",
"saved": false,
"tags": ["parking", "wc", "kitchen"]
}
]
And I'd like to filter list by tags. e.g when I click on checkbox the list should give me filtered array based on selected value. When I click on checkbox it creates new array with clicked value e.g let checked = ["parking", "kitchen"] which is to match tags object in bnb
<input type="checkbox" value="parking">Parking<br>
<input type="checkbox" value="kitchen">Kitchen<br>
...
I know that I could use filter function like this, but it doesn't filter array.
let bnblist = bnb.filter(o => o.tags.includes(checked);
Other thing is that filtering should subtract array e.g if I selected ["parking", "kitchen"] it should return id5 and id7. Basically it's similar to && operator, but it needs two values to be selected and there are multiple checkboxes and I don't know how many user is goint to select.
let bnblist = bnb.filter(o => o.tags === "parking" && "kitchen");
You can use this code.
let bnb = [{"id":"id1","img":["tres","dos","uno"],"title":"one","city":"texas","saved":false,"tags":["bath"]},{"id":"id2","img":["quatro","uno","tres"],"title":"two","city":"denver","saved":false,"tags":["wc"]},{"id":"id3","img":["uno","dos","tres"],"title":"three","city":"vancouver","saved":false,"tags":["wc","bath"]},{"id":"id4","img":["dos","uno","tres"],"title":"four","city":"berlin","saved":false,"tags":["bath","parking"]},{"id":"id5","img":["tres","uno","quatro"],"title":"five","city":"paris","saved":false,"tags":["loft","parking","kitchen"]},{"id":"id6","img":["quatro","uno","tres"],"title":"six","city":"barcelona","saved":false,"tags":["bath","wc"]},{"id":"id7","img":["uno","tres","quatro"],"title":"seven","city":"seul","saved":false,"tags":["parking","wc","kitchen"]}]
let checked = ["parking", "kitchen"]
let bnblist = bnb.filter(o => {
let res = 0;
o.tags.forEach(tag => res += checked.includes(tag));
return res == checked.length && o;
})
console.log(bnblist)
You just need to add every method that will iterate over checked tags and check if those are included inside tags of the current element of filter.
const bnb = [{"id":"id1","img":["tres","dos","uno"],"title":"one","city":"texas","saved":false,"tags":["bath"]},{"id":"id2","img":["quatro","uno","tres"],"title":"two","city":"denver","saved":false,"tags":["wc"]},{"id":"id3","img":["uno","dos","tres"],"title":"three","city":"vancouver","saved":false,"tags":["wc","bath"]},{"id":"id4","img":["dos","uno","tres"],"title":"four","city":"berlin","saved":false,"tags":["bath","parking"]},{"id":"id5","img":["tres","uno","quatro"],"title":"five","city":"paris","saved":false,"tags":["loft","parking","kitchen"]},{"id":"id6","img":["quatro","uno","tres"],"title":"six","city":"barcelona","saved":false,"tags":["bath","wc"]},{"id":"id7","img":["uno","tres","quatro"],"title":"seven","city":"seul","saved":false,"tags":["parking","wc","kitchen"]}]
let checked = ["parking", "kitchen"]
const result = bnb.filter(({ tags }) => checked.every(tag => tags.includes(tag)))
console.log(result)
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 6 years ago.
I am working on an ionic 1 app, I do not know if there is a way but for example, here, is there a way I can dynamically assign a parent in this object depending on the value of the Variable?
var contact = {
"Name": "John Doe",
"available": true,
Variable: [
{
"Location": "Home",
"Number": "33"
},
{
"Location": "Work",
"Number": "22"
}
]
};
Lets say Variable = "friends" Then
var contact = {
"Name": "John Doe",
"available": true,
"Friends": [
{
"Location": "Home",
"Number": "33"
},
{
"Location": "Work",
"Number": "22"
}
]
};
I know I can use ES6, the Computed Property Names [Variable] but they are not working on older devices. Is there any alternative method?
Just plain old
var contact = {
"Name": "John Doe",
"available": true,
};
contact[Variable] = [
{
"Location": "Home",
"Number": "33"
},
{
"Location": "Work",
"Number": "22"
}
]
Having a thorny problem and only see similar but also simpler solutions on SO.
Is is possible to generate a dynamic key AND dynamic values using JS/JSON?
For instance, let's say I have JSON like this:
{
"email": "user#someco.com",
"firstname": "Bob",
"lastname": "Smith",
"company": "ACME",
"custom": {
"services": [
{
"name": "svc1",
"desc": "abcdefg",
"selected": "true",
"status": "None"
},
{
"name": "svc2",
"desc": "abcdefg",
"selected": "true",
"status": "None"
},
{
"name": "svc3",
"desc": "abcdefg",
"selected": "false",
"status": "None"
},
{
"name": "svc4",
"desc": "abcdefg",
"selected": "false",
"status": "None"
}
],
"fields": [
{
"name": "Products",
"desc": "abcdef",
"type": "multi",
"values": [
{
"name": "Product1",
"desc": "abcdef"
},
{
"name": "Product2",
"desc": "abcdef"
}
],
"services": [
"svc1",
"svc2",
"svc3"
]
},
{
"name": "Wines",
"desc": "abcdef",
"type": "multi",
"values": [
{
"name": "Wine 1",
"desc": "abcdef"
}
],
"services": [
"svc4"
]
},
{
"name": "Fruits",
"desc": "abcdef",
"type": "multi",
"values": [
{
"name": "Fruit 1",
"desc": "abcdef"
},
{
"name": "Fruit 2",
"desc": "abcdef"
}
],
"services": [
"svc4"
]
}
]
}
};
I need to go into the fields and for each field (products, wines, fruits) see if a given service is contained within so that I can go back and generate a product or wine or fruit for each service that requires it. But I don't want to repeat the services names more than once. The resulting JSON should look something like this:
{"svc1":["Products"], "svc2":["Products"], "svc3":["Products"], "svc4":["Fruits", "Wines"]}
The hope would be that to generate a dynamic list in Angular I can just turn and loop back through this JSON, pulling out the values for each product, fruit, wine, whatever.
I've been trying a lot of nested for loops and the like but whenever I get more than one layer down the dynamism seems to stop. I'm guessing that for this to work I need to move between JS Objects and JSON?
Right now I'm trying something like this, which isn't quite working, stringify or no. And maybe I'm flip-flopping too much between JSON and JS Objects:
var outObj = [];
var fieldItems;
$.each(jsonObj.custom.fields, function(key, item) {
fieldItems = item;
fieldItems.name = item.name;
$.each(fieldItems.services, function(key, item) {
var serviceName = item;
//check to see if the serviceName already exists
if (outObj.indexOf(serviceName) > -1) {
outObj.serviceName.push(fieldItems.name);
} else {
outObj.push(serviceName);
}
});
});
JSON.stringify(outObj);
console.log("outObj " + outObj);
I get "can't read property 'push' of undefined" errors and the like. Seems this should be possible from a single nested loop, but maybe I need to just do two passes? Any other suggestions?
To me it sounds like overcomplicated solution. You can use basic array methods of javascript to filter out required structure. I am not sure what profiling_value in the presented snippet, so I started from the object structure in OP
var desiredResult = jsonObj.custom.services.reduce(function(result, service){
result[service.name] = jsonObj.custom.fields.filter(function(field){
return field.services.indexOf(service.name) >= 0;
}).map(function(field){ return field.name; });
return result;
}, {});
This gives the expected result for mentioned object.
reduce is required to iterate over all services and accumulate result in one object. Then for each service fields are iterated to filter out only those that contain link to this service. And finally list of filtered fields is transformed (map) into list of strings - their names - and inserted into accumulator