Compare Objects and find matched value - javascript

Basically, I want to compare some objects and re-group if the value of specific property is matched.
As following code, If types are same, I want to regroup as one array of object.
var obj = [{id: 94 , type: 'silver'}, {id:95,type:'gold'} , {id:96, type: 'silver'} ]
return newObj = [{id: 94 , type: 'silver'},{id:96, type: 'silver'} ]
I would like to use lodash or underscore If It’s possible.
Anyone have great idea? Thanks.

Do you want something like this? https://jsbin.com/negeqineje/edit?js,console
var metals = [
{id: 94 , type: 'silver'},
{id: 95, type:'gold'},
{id: 96, type: 'silver'},
{id: 97, type: 'platinum'},
];
var grouped = metals.reduce(function (grouped, metal) {
grouped[metal.type] = metals.filter(function (_metal) {
return (metal.type === _metal.type);
});
return grouped;
}, {});

Related

Comparing two parameters of an object array in JavaScript

I am completing a homework assignment where I will have to filter an array into a new array for a vehicle with the term of “ford” in it. The assignment wants the format to be in ES6 using the arrow function syntax so something like
const arr = [
{name: “Honda”, type: “Accord”},
{name: “ford”, type: “fusion”},
{name: “Toyota”, type: “Camry”}
]
const newArr = [...arr].filter(value => value === ‘ford’);
Console.log(newArr);
I know this is incorrect and wouldn’t actually get the name of the vehicle that has “ford” in it but I’m giving an example on how they would want it formatted.
You need value.name also check the quotes. In this case there may not be any use of the spread operator
const arr = [{
name: 'Honda',
type: 'Accord'
},
{
name: 'ford',
type: 'fusion'
},
{
name: 'Toyota',
type: 'Camry'
}
]
const newArr = arr.filter(value => value.name === 'ford');
console.log(newArr);

How to get JSON keys and add extra fields?

I'm trying to get the key of these json objects in order to create a new object with extra filed to create table headers in a React app. JSON data:
let example = [
{
id: 1,
city: 'New York',
},
{
id: 2,
city: 'Paris',
},
]
The function:
getKeys() {
return example.map((key) => {
return {
cityName: key, // gets the whole array
capital: false,
};
});
}
I tries Object.keys( example);, it returns integers; 0, 1.
How can I get the keys in this case? Thanks.
You are trying to map the keys for an array since example is an array. If the data are consistent throughout the array get the first element example[0] and do Object.keys().
So Object.keys(example[0])
There's no need to get the keys if you just want to add a property to the items in the array. I think there's a misunderstanding about .map, which gives you a single item/object in the array, not the keys.
Something like this perhaps?
let example = [{
id: 1,
city: 'New York',
}, {
id: 2,
city: 'Paris',
}];
const modifiedArray = function(arr) {
return arr.map(item => {
return {
id: item.id,
cityName: item.city,
capital: false,
};
})
}
const newArray = modifiedArray (example);
console.log(newArray )

Javascript How to push item in object

var data = {items: [
{id: "1", name: "Snatch", type: "crime"}
]};
And I would like to add the mark's key.
So the result would be:
var data = {items: [
{id: "1", name: "Snatch", type: "crime", mark:"10"}
]};
How can I do ?
I tried to do data.items.push({"mark": "10"}) but it adds another object which is not what I want.
Thanks.
Access the correct index and simply set the property
data.items[0].mark = "10";
You may not need push here because you want to create a new key to n existig object. Here you need dot (.) to create a new key
var data = {
items: [{
id: "1",
name: "Snatch",
type: "crime"
}]
};
data.items[0].mark = "10";
console.log(data)
And, if you want add “mark” property to all the items:
data.items.forEach(function(item, index) {
data.items[index].mark = 10;
}

json object from javascript nested array

I'm using a nested array with the following structure:
arr[0]["id"] = "example0";
arr[0]["name"] = "name0";
arr[1]["id"] = "example1";
arr[1]["name"] = "name1";
arr[2]["id"] = "example2";
arr[2]["name"] = "name2";
now I'm trying to get a nested Json Object from this array
arr{
{
id: example0,
name: name00,
},
{
id: example1,
name: name01,
},
{
id: example2,
name: name02,
}
}
I tought it would work with JSON.stringify(arr); but it doesen't :(
I would be really happy for a solution.
Thank you!
If you are starting out with an array that looks like this, where each subarray's first element is the id and the second element is the name:
const array = [["example0", "name00"], ["example1", "name01"], ["example2", "name02"]]
You first need to map it to an array of Objects.
const arrayOfObjects = array.map((el) => ({
id: el[0],
name: el[1]
}))
Then you can call JSON.stringify(arrayOfObjects) to get the JSON.
You need to make a valid array:
arr = [
{
id: 'example0',
name: 'name00',
},
{
id: 'example1',
name: 'name01',
},
{
id: 'example2',
name: 'name02',
}
];
console.log(JSON.stringify(arr));
Note that I am assigning the array to a variable here. Also, I use [] to create an array where your original code had {}.

Exclude given objects from array

I have the below array. I am attempting to exclude certain objects from this array in processing.
For example. I would like to exclude the type 'dog' and only use any object that is of type duck.
I'd like to do this using underscore/lodash but will use plain JS if need be.
animals: [
{
type: 'duck',
name: 'quack',
},
{
type: 'duck',
name: 'quieck',
},
{
type: 'dog',
name: 'bark',
},
]
The Underscore/LoDash way, would be just
var result = _.where(animals, {type: 'duck'});
I suppose your array represents variable animals. You can use Array.prototype.filter() function. If you want all ducks:
const animals = [
{ type: 'duck', name: 'quack' },
{ type: 'duck', name: 'quieck' },
{ type: 'dog', name: 'bark' },
];
const ducks = animals.filter(o => o.type === 'duck');
or if you want to exclude all dogs:
const withoutDogs = animals.filter(o => o.type !== 'dog');
I used ES6 syntax. ES5 equivalent would be:
var ducks = animals.filter(function(o) { return o.type === 'duck' });

Categories

Resources