Example:
Animals = [{Name: 'Dog', Id: 0},
{Name: 'Cat', Id: 1},
{Name: 'Mouse', Id: null}]
How to take all objects where Id isn't null into new array?
Expected output:
NewArray = [{Name: 'Dog', Id: 0},
{Name: 'Cat', Id: 1}]
Try with _.filter
var Animals = [{Name: 'Dog', Id: 0},{Name: 'Cat', Id: 1},{Name: 'Mouse', Id:null}]
var newArray =_.filter(Animals ,a=> a.Id != null)
console.log(newArray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.min.js"></script>
Related
So I have two arrays with the same length, but not entirely the same data as follows:
Array1: [{name: john, num: 030}, {name: david, num: 130}, {name: john, num: 200}, {name: jane, num: 500}]
Array2: [{name: john, num: 030}, {name: david, num: 130}, {name: jane, num: 500}, {name: '', num: ''}]
Array2 only has element where num matches Array1 num
Is there a way to make sure that these two arrays match their indexes even if the data does not match
for example, their index will look like this
Array1: [{name: john, num: 030}, {name: david, num: 130}, {name: john, num: 200}, {name: jane, num: 500}]
Array2: [{name: james, num: 030}, {name: frank, num: 130}, {name: '', num: ''}, {name: kate, num: 500},]
This means they match by index, and order is maintained.
The main goal is that Array2 maintains the order of Array1.
According to the description seen in the question, list two logics
The value of attribute num is unique in each array
The only exception is an empty string
I implemented it according to the above logic, you can see if it helps you, if the logic does not meet your actual needs, you can provide more details and clear logic, I will adjust it
const array1 = [{name: 'john', num: '030'}, {name: 'david', num: '130'}, {name: 'john', num: '200'}, {name: 'jane', num: '500'}];
let array2 = [{name: 'david', num: '130'}, {name: 'jane', num: '500'}, {name: 'john', num: '030'}, {name: '', num: ''}];
const originalLength = array2.length;
const originalArr = array2.slice(0, originalLength);
array2.length = originalLength * 2;
const provide = (arr, field, index) => {
let result = arr.filter(a => a[field] == array1[index][field]);
if(result.length == 0){
result = arr.filter(a => a[field] == '');
}
return result[0];
};
for(let i=0; i<array1.length ; i++)
{
const item = provide(originalArr, 'num', i);
array2[i] = item;
}
array2.length = originalLength;
console.log(JSON.stringify(array2));
I need to get the expectedOutput array, which consists of the objects with the higher amount number. Thanks in advance.
let arr1 = [{name: 'Almendras', amount: 0},{name: 'Nueces', amount: 0}, {name: 'Chocolate', amount: 0}];
let arr2 = [{name: 'Almendras', amount: 2}];
let expectedOutput = [{name: 'Almendras', amount: 2}, {name: 'Nueces', amount: 0}, {name: 'Chocolate', amount: 0}];
Im tried by this way:
console.log(arr2.filter(x => x.amount > arr1[x].amount));
Since the example you gave uses the same index in both arrays, you could map through the first array and index compare the second array and return one or the other:
var res = arr1.map((v, index) => v.amount > arr2[index].amount ? v : arr2[index]);
If either array can be larger than the other you could find the larger array, loop through it and compare it with the smaller one and add the objects to a new array until you reach the end, and then add the remaining objects from the larger array.
You can use Array#map, which provides the index as the second argument to the callback, and use a ternary operator the compare the element at the index in both arrays.
let arr1 = [{name: 'Almendras', amount: 0},{name: 'Nueces', amount: 0}, {name: 'Chocolate', amount: 0}];
let arr2 = [{name: 'Almendras', amount: 2}, {name: 'Nueces', amount: 0}, {name: 'Chocolate', amount: 1}];
let res = arr1.map((curr,idx)=>curr.amount > arr2[idx].amount?curr:arr2[idx]);
console.log(res);
If you know that both arrays are sorted according to the inner object name key, then the other answers which compare indices are probably the best method. However, if you know that the arrays could be unsorted, here is a potential solution.
Note: If you are dealing with arrays of varying sizes, you would have to filter or iterate through both arrays.
let arr1 = [{name: 'Almendras', amount: 0}, {name: 'Nueces', amount: 0}, {name: 'Chocolate', amount: 0}];
let arr2 = [{name: 'Almendras', amount: 2}, {name: 'Chocolate', amount: 1}, {name: 'Nueces', amount: 0}];
let expectedOutput = [{name: 'Almendras', amount: 2}, {name: 'Nueces', amount: 0}, {name: 'Chocolate', amount: 1}];
let actualOutput = arr1.map((ele1) => {
let ele2 = arr2.find((e) => e.name === ele1.name);
if(ele2) {
if(ele2.amount >= ele1.amount) {
return ele2;
}
}
return ele1;
});
console.log("actual: ", actualOutput);
console.log("expected: ", expectedOutput);
I have a JavaScript array of objects to sort and arrange it as a new object array preserving the order.
My array of objects. I get them ordered in the way that I want. The index of the objects are 0,1 & 2 in this.
[{0: {name: 'Joel', age:25, id: 2}}, {1: {name: 'Sam', age: 23, id: 4}}, {2: {name: 'Tim', age:27, id: 3}}]
What I want is to make 'id' the index value. And I want it in ascending order of the 'name' (Preserving the above order)
[{2: {name: 'Joel', age:25, id: 2}}, {4: {name: 'Sam', age: 23, id: 4}}, {3: {name: 'Tim', age:27, id: 3}}]
I tried using this function.
for (i in members) {
member[members[i].id] = members[i];
}
But it failed. The output was
[{}, {}, {name: "Joel", age:25, id: 2}, {name: "Tim", age:27, id: 3}, {name: "Sam", age: 23, id: 4}]
I tried using forEach and sort methods too. But all failed.
Is there any way to accomplish this in JavaScript.
You can do it with help of map and sort.
let obj = [{0: {name: 'Joel', age:25, id: 2}}, {1: {name: 'Sam', age: 23, id: 4}}, {2: {name: 'Tim', age:27, id: 3}}]
let op = obj.map((e,index)=>{
return{
[e[index].id] : e[index]
}
}).sort((a,b)=> Object.keys(a) - Object.keys(b))
console.log(op)
if I have this array of objects. I am looking to add the missing keys from array of objects. For each missing key there should be three entries in the final array with name field empty.
myArray = [{id: 1, name: 'John'},
{id: 1, name: 'Ray'},
{id: 1, name: 'Elliot'},
{id: 3, name: 'Elli'},
{id: 3, name: 'Smith'},
{id: 3, name: 'John'}]
my expected output should loook like:
expectedArray = [{id: 1, name: 'John'},
{id: 1, name: 'Ray'},
{id: 1, name: 'Elliot'},
{id: 2, name: ''},
{id: 2, name: ''},
{id: 2, name: ''},
{id: 3, name: 'campbell’},
{id: 3, name: 'Smith'},
{id: 3, name: 'John'},
{id: 4, name: ''},
{id: 4, name: ''},
{id: 4, name: ''}]
I have tried different ways to acheive this task but I failed to do. Can you please help me how I can do this? Thanks.
I'm assuming, you mean that if all properties of a given object are not the same as an object found in the list of objects, then add it to the list.
You could do so by
const givenObject = {
id: 4,
name: ''
};
myArray.forEach(obj => {
if (obj.id !== givenObject.id || obj.name !== givenObject.name) {
myArray.push(givenObject);
myArray.push(givenObject);
myArray.push(givenObject);
}
})
I want to transform:
[
{id: 1, name: 'one', desc: 'one'},
{id: 2, name: 'two', desc: 'two'},
{id: 3, name: 'three', desc: 'three'}
]
to
{
1: {id: 1, name: 'one', desc: 'one'},
2: {id: 2, name: 'two', desc: 'two'},
3: {id: 3, name: 'three', desc: 'three'}
}
What is the most efficient/performant way to do this? Some options would be:
1) https://github.com/gaearon/normalizr
2) d3.nest()
3) const object = {}; array.forEach(item => { object[item.id] = item });
I like Array.prototype.reduce() solutions. Check this out
var arr = [{id: 1, name: 'one', desc: 'one'}, {id: 2, name: 'two', desc: 'two'}, {id: 3, name: 'three', desc: 'three'}],
obj = arr.reduce((p,c) => {p[c.id] = c; return p},{});
document.write("<pre>" + JSON.stringify(obj,null,2) + "</pre>");
You can also use a simple loop:
var arr = [{id: 1, name: 'one', desc: 'one'}, {id: 2, name: 'two', desc: 'two'}, {id: 3, name: 'three', desc: 'three'}],
obj = {}
for(var item of arr) obj[item.id] = item;
Usually loops are faster than ES5 array methods because they don't have to call a function at each iteration.
I would say that it is:
const obj = Object.assign( {}, array );
Although, I haven't compared its performance to your options.
try this:
ar = [
{id: 1, name: 'one', desc: 'one'},
{id: 2, name: 'two', desc: 'two'},
{id: 3, name: 'three', desc: 'three'}
]
var result = ar.reduce((ac, x) => {return ac[x.id] = x , ac ;}, {})
document.write( JSON.stringify(result) )
but remember that the keys are strings and you're dealing with an object not array...