I have an array of objects as shown below.
var myArray = [
{
Data: '455',
Note: 'tre',
Id: '4'
},
{
Data: '456',
Note: 'bre',
Id: '5'
},
{
Data: '457',
Note: 'cre',
Id: '6'
}
];
I also have this array
Percent = [ '10', '20', '30'],
Can someone please let me know how do i add this array elements into the array of objects. tHe expected output is as follows.
var myArray = [
{
Data: '455',
Note: 'tre',
Id: '4',
Percent: '10'
},
{
Data: '456',
Note: 'bre',
Id: '5',
Percent: '20'
},
{
Data: '457',
Note: 'cre',
Id: '6',
Percent: '30'
}
];
Assuming Percent always contains the same number of items as myArray, loop over myArray and assign the correct value from Percent like this:
myArray.forEach(function(object,index) {
object.Percent = Percent[index];
});
Same assumption as #Adam, but using ES6 arrow function feature:
myArray.map((x,i) => x.Percent = percent[i]);
here is a plunker : link
Don't forget to check browser compatibility : here
Related
This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 4 months ago.
I have an array of objects that I'm trying to filter by a list of values:
var idlist = [
{
id: '1',
},
{
id: '3',
},
{
id: '2',
},
{
id: '6',
},
]
var values = ['1', '6', '3']
one way i've found to do this is to convert my list into an object and then filter using
value.filter(k =>
obj.find(v => v.id === k.id)
)
However, I feel like that's an extra step I don't need. Is there a simpler solution?
The output i'm looking for is
new idlist
[
{
id: '1',
},
{
id: '3',
},
{
id: '6',
},
]
Thanks!
Simply use
idlist.filter(f => values.includes(f.id))
var idlist = [{
id: '1',
},
{
id: '3',
},
{
id: '2',
},
{
id: '6',
},
]
var values = ['1', '6', '3']
console.log(idlist.filter(f => values.includes(f.id)))
I have an array of objects as myData which consists of the below JSON structure:
const data = {
myData: [
{ Type: 'REAL', Tenure: '12', Name: 'WEBPAGE' },
{ Type: 'REAL', Tenure: '24', Name: 'SERVER' },
{ Type: 'REAL', Tenure: '12', Name: 'WEBPAGE' },
],
};
I want to get rid of duplicate entries by keeping performance in mind as this array could be of length 1000+ using Javascript.
The Expected Output that I am looking for is as follow since myData[0] && myData[2] are duplicate here:
const result = {
myData: [
{ Type: 'REAL', Tenure: '24', Name: 'SERVER' },
{ Type: 'REAL', Tenure: '12', Name: 'WEBPAGE' },
],
};
You can use Lodash uniq function for more information please check https://lodash.com/docs/3.10.1#uniq
I have array like this .
const test = [
{ student: { id : '1', Name: 'A' }, marks: {
id: '2', Name: 'B'
} },
{ student: {
id : '3', Name: 'C' }, marks: { id: '4', Name: 'D' } }
]
Now, from this array of object , I am trying to create two diff arrays which will be having seperate student and marks keys .
const student = [{"student":{"Id": {value: "A"}}}, {"student":{"Id": {value: "B"}}}]
and for marks
const marks = [{"marks":{"Id": {value: "C"}}}, {"marks":{"Id": {value: "D"}}}]
SO, Here what I tried is
test.map((index,item) => {
return [item.student]
})
can any one help me with this ?
Thanks.
You want a new object returned, not a sub array.
Following uses destructuring to simplify the returned object
const test = [
{ student: { id : '1', Name: 'A' }, marks: {
id: '2', Name: 'B'
} },
{ student: {
id : '3', Name: 'C' }, marks: { id: '4', Name: 'D' } }
]
const students = test.map(({student}) => ({student}))
const marks = test.map(({marks}) => ({marks}))
console.log(students)
console.log(marks)
I'm trying to remove all items if they match with array values but it's removing only one item. How can i remove all items with filter method or what is the best way to achieve this.
let data = [
{
id: '1',
title: 'ABC'
},
{
id: '2',
title: 'DEF'
},
{
id: '3',
title: 'GHI'
},
{
id: '4',
title: 'JKL'
},
{
id: '5',
title: 'MNO'
}
]
data = data.filter(post => {
let remove = ['2', '4', '5']
for(let i = 0; i < remove.length; i++) {
return post.id !== remove[i]
}
})
console.log(data)
Thanks
you should return false if you want to remove item from array
let data = [
{
id: '1',
title: 'ABC'
},
{
id: '2',
title: 'DEF'
},
{
id: '3',
title: 'GHI'
},
{
id: '4',
title: 'JKL'
},
{
id: '5',
title: 'MNO'
}
]
let remove = ['2', '4', '5']
data = data.filter(post => {
return !remove.includes(post.id);
})
console.log(data)
All the notice are in the snippet's comment
let data = [ { id: '1', title: 'ABC' }, { id: '2', title: 'DEF' }, { id: '3', title: 'GHI' }, { id: '4', title: 'JKL' }, { id: '5', title: 'MNO' } ]
const remove = ['2', '4', '5']
// `indexOf` is from ES5
data = data.filter(post => remove.indexOf(post.id) === -1)
console.log(data)
// `includes` is from ES7
data = data.filter(post => !remove.includes(post.id))
console.log(data)
// this will recreate the array ['2', '4', '5'] 5 times
data = data.filter(post => !['2', '4', '5'].includes(post.id))
console.log(data)
There is no need to use for loop inside of filter.
Instead it is possible to use some method inside of filter. The some method checks whether at least one element satisfies condition inside of provided function. So unnecessary iteration will be avoided:
data.filter(f => !remove.some(s => s == f.id))
An example:
let data = [
{
id: '1',
title: 'ABC'
},
{
id: '2',
title: 'DEF'
},
{
id: '3',
title: 'GHI'
},
{
id: '4',
title: 'JKL'
},
{
id: '5',
title: 'MNO'
}
]
let remove = ['2', '4', '5']
console.log(data.filter(f => !remove.some(s => s == f.id)));
I'll suggest using includes rather then a nested for loop.
You should also move the remove var outside of the loop, so it's not reinitialised every time.
The callback to the filter method is a predicate. If the condition evaluates to true, the current value in the iteration will be returned. In your case, you want to return if the current value is not in the remove array.
let data = [
{
id: '1',
title: 'ABC'
},
{
id: '2',
title: 'DEF'
},
{
id: '3',
title: 'GHI'
},
{
id: '4',
title: 'JKL'
},
{
id: '5',
title: 'MNO'
}
]
const remove = ['2', '4', '5']
data = data.filter(post => {
return !remove.includes(post.id)
})
console.log(data)
Let's assume I've collection which has the following structure;
{
id:1,
name: 'name1',
students: ['1', '2' , '3']
}
{
id:2,
name: 'name2',
students: ['11', '22' , '33']
}
...
I want to get all students element in one array.
I can do as:
db.collection.find({}, {students: 1, _id:0})
This returns me an array as;
result = [
{students: ['1', '2', '3']},
{students: ['11', '22','33']},
]
However I want to get result = ['1', '2', '3', '11', '22','33'];
What is the most efficient way to get result just like this?
If you want to go JavaScript way, Use Array.prototype.reduce
The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
Try this:
var result = [{
students: ['1', '2', '3']
}, {
students: ['11', '22', '33']
}, ];
var merged = result.reduce(function(a, b) {
return a.students.concat(b.students);
});
console.log(merged);
You can use the aggragation framework:
db.collection.aggregate([{
$unwind: '$students'
},{
$group: {
_id: '1',
students: {
$push: '$students'
}
}
}])
Try with aggregation framework.
db.collection.aggregate([
{$project:{_id:0, students:1}},
{$group:{_id:null, result:{$push:"$$ROOT"}}},
{$project:{_id:0, result:1}}
])
This will emit:
{
"result" : [
{
"students" : [
"1",
"2",
"3"
]
},
{
"students" : [
"11",
"22",
"33"
]
}
]
}