Javascript - To remove duplicates in array object for ES5 [duplicate] - javascript

This question already has answers here:
How to call reduce on an array of objects to sum their properties?
(23 answers)
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 3 years ago.
I have the an array, listPeople and to eliminate duplicates I use the following code in ES6:
var listPeople = [{
"nomCustomer": "Fran",
"attrName": "aaa"
},
{
"nomCustomer": "John",
"attrName": "ccc"
},
{
"nomCustomer": "Paul",
"attrName": "ddd"
},
{
"nomCustomer": "Paul",
"attrName": "ddd"
}
];
var list = listPeople.reduce((unique, o) => {
if (!unique.some(obj => obj.nomCustomer === o.nomCustomer && obj.attrName === o.attrName)) {
unique.push(o);
}
return unique;
}, []);
console.log(list)
What would it be like for ES5?

Related

Getting the object from an array on the basis of id [duplicate]

This question already has answers here:
How to find an appropriate object in array by one of its properties
(3 answers)
Closed 29 days ago.
I have a array as follows :
data = [
{
"data": {
"id":1,
"vol":"0.0"
"requiredId":100
"details":{
"ABC":"8.30",
"OFG":"13.85",
"SPG":"70.80"
}
}
},
{
"data": {
"id":2,
"vol":"1.0",
"requiredId":2
"details":{
"ABC":"3.30",
"OFG":"15.85",
"SPG":"70.80"
}
}
}
]
I just want to get the object from this array where id is equal to requiredId. How can I do that?
data.find(item => item.data.id === item.data.requiredId)

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)

Javascript proper way to .push array [duplicate]

This question already has an answer here:
javascript push returning number instead of object [duplicate]
(1 answer)
Closed 3 months ago.
How to use .push array in proper way using javascript?
my code
let persons = [{
"person1": "person1"
},
{
"person1": "person1"
}
]
let addPerson = []
addPersn.push({
"person1": "person2"
})
let allPerson = persons.push(addPerson)
console.log(allPerson)
expected behavior
{
"person1": "person1"
},
{
"person1": "person1"
},
{
"person1": "person1"
}
current result
3
How to use .push array in proper way using javascript?How to use .push array in proper way using javascript?
Try this
let persons = [
{
person1: "person1",
},
{
person2: "person2",
},
];
const newPerson = {
person3: "person3",
}
persons.push(newPerson);
console.log(persons);

Sort an array by names with sort in TypeScript [duplicate]

This question already has answers here:
Sorting an array in Javascript based on predefined order
(3 answers)
Sort an array of object by a property (with custom order, not alphabetically)
(7 answers)
Sorting on a custom order
(4 answers)
Closed 5 months ago.
var arr = [{
name: 'low'
},
{
name: 'middle'
},
{
name: 'urgent'
},
{
name: 'high'
},
{
name: 'none'
}
]
How do i sort this array based on the name and the order should be 1.urgent, 2.high, 3.medium, 4.low, 5.none

Partially replace value of key-val pair in an object [duplicate]

This question already has answers here:
Es6 merge two objects using spread not working
(5 answers)
How to deep merge instead of shallow merge?
(47 answers)
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 11 months ago.
Given I have the following object:
let obj = {
cat: "paws",
dogs: {
lab: "Christopher",
otherLab: "Lady Buba"
}
}
let objTwo = {
dogs: {
lab: "some new name"
}
}
How can I merge the above two objects so that the final result is:
{
cat: "paws",
dogs: {
lab: "some new name",
otherLab: "Lady Buba"
}
}
I've attempted to use spread operators with let finalObj = {...obj, ...objTwo}, but this entirely replaces the value of dogs, so that the end result is:
{
cat: "paws",
dogs: {
lab: "some new name",
}
}
which isn't what I want.

Categories