Angular 8 : Merging two objects in an array [duplicate] - javascript

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
How to convert an array of objects to object with key value pairs
(6 answers)
Closed 3 years ago.
I have a couple of objects inside of an array :
array1 =[{name : "Cena", age : 44},{job : "actor", location : "USA"}]
is there a way for me to merge these two objects to get something like :
array2 =[{name : "Cena", age : 44, job : "actor", location : "USA"}]
I tried looping through the elements but it is not a good option if the object is a big one, I guess. Any good solution using typescript?

You can use Array.prototype.reduce:
const array1 = [{ name: "Cena", age: 44 }, { job: "actor", location: "USA" }];
const array2 = [array1.reduce((acc, cur) => ({ ...acc, ...cur }))];
console.log(array2);

Related

Redefine object properties keys starting with prefix [duplicate]

This question already has answers here:
What's the quickest way to rename all property names in a Javascript object?
(3 answers)
How to remove part of a string?
(7 answers)
JavaScript: Object Rename Key
(35 answers)
Closed 2 months ago.
If I have the following object, how can I redefine the keys to remove the prefix "Class"?
{
ClassName: "John",
ClassEmail: "john#doe.com",
ClassPhone: "1234567"
}
so it becomes
{
Name: "John",
Email: "john#doe.com",
Phone: "1234567"
}
Is there any easy way?
const obj = {
ClassName: "John",
ClassEmail: "john#doe.com",
ClassPhone: "1234567"
}
const newObj = Object.entries(obj).reduce((acc, item) => ({ ...acc,
[item[0].replace(/Class/g, "")]: item[1]
}), {})
console.log(newObj)

Is there any good way to get new Array from new Array when things in Array has common factors?(react.js) [duplicate]

This question already has answers here:
javascript filter array of objects
(8 answers)
Get JavaScript object from array of objects by value of property [duplicate]
(17 answers)
Closed 1 year ago.
Suppose there is an Array that has some objects like below
[{name:"Bruce wayne" age:42 DOA:true},{name: "Dick grayson" age:28 DOA:true},{name:"Jason todd" age:24 DOA:false}]
and I want to get a new Array with Objects which DOA is true (bruce and dick).
Is there any good API or something to do this?
your help is going to be appreciated Thx!
You can simply use Array.prototype.filter:
const data = [{
name: "Bruce wayne",
age: 42,
DOA: true
}, {
name: "Dick grayson",
age: 28,
DOA: true
}, {
name: "Jason todd",
age: 24,
DOA: false
}];
console.log(data.filter(obj => obj.DOA));
Mind that your JSON was also invalid.

Convert array values to objects [duplicate]

This question already has answers here:
Convert array of strings into an array of objects
(6 answers)
Closed 1 year ago.
I have this code
const jsonArray = [1,2,3,4,5];
I want to convert it so I can get
{
"id" : 1
},
{
"id" : 2
},
{
"id" : 3
},
{
"id" : 4
},
{
"id" : 5
}
So I need to add "id" key for all the objects outputs
Try the map function
jsonObjectArray = jsonArray.map((ele) => {return {"id": ele}})
You can use map.
jsonArray.map(id=>({id}))
You could use Array.prototype.map() for that:
const jsonArray = [1,2,3,4,5];
const arrayofObjects = jsonArray.map(e => ({id: e}))
console.log(arrayofObjects);

Find within array of objects the item having maximum value of certain property and return another property of that item [duplicate]

This question already has answers here:
From an array of objects, how to return property `b` of the object that has the highest property `a`?
(4 answers)
Finding the max value of an attribute in an array of objects
(21 answers)
Closed 2 years ago.
Trying to create a function to return the Name if the price is the highest using javascript. Still learning.
{
Name: "Effective Yoga Habits",
type: "book",
price: 10.99
},
{
Name: "Yoga kit",
type: "product",
price: 199.99
},
{
Name: "Medative surroundings",
type: "CD",
price: 6.00
}
]
If anyone can help I would appreciate it. Thank you in advance
You may use Array.prototype.reduce() to traverse your array and have accumulator to store the record with greatest price value:
const src = [{Name:"Effective Yoga Habits",type:"book",price:10.99},{Name:"Yoga kit",type:"product",price:199.99},{Name:"Medative surroundings",type:"CD",price:6.00}],
result = src
.reduce((r, {Name,price}) =>
price > r.price ? {Name,price} : r)
.Name
console.log(result)

Efficient way to get object from array based on key [duplicate]

This question already has answers here:
Find a value in an array of objects in Javascript [duplicate]
(20 answers)
Get JavaScript object from array of objects by value of property [duplicate]
(17 answers)
Closed 4 years ago.
I have an array of objects as:
0: {id: "1", name: "Tab1", address: "123 Street"}
1: {id: "2", name: "Tab2", address: "456 Avenue"}
2: {id: "3", name: "Tab3", address: "789 st"}
I want to grab a particular object from the above array based on the "name" key. For example, if I pass the key as "Tab1" it should return me:
0: {id: "1", name: "Tab1", address: "123 Street"}
I can loop through the array and get the values but wanted to know is there any simpler/efficient way to get the desired data.
You can use Array.prototype.find like this
const searchArray = key => array.find(({name})=> {
return name === key;
});
console.log(searchArray("Tab1"));

Categories