JS: Put elements to array beginning on condition - javascript

I have an array something like this:
[
{id: 5, attr: 99},
{id: 7, attr: null},
{id: 2, attr: 8},
{id: 9, attr: 3},
{id: 4, attr: null}
]
What would be the most efficient to put all objects with attr === null at the beginning of the array, without changing order of other elements in the array? (e.g. I need ids to go in this order: 7, 4, 5, 2, 9 (the order of 7 and 4 don't matter, but the order of 5, 2, 9 must never change.)
Simple fiddle to test your code: http://jsfiddle.net/8GEPC/

Could used built in filter methods that require two loops or one while loop to find the elements and remove them.
var x = [
{id: 5, attr: 99},
{id: 7, attr: null},
{id: 2, attr: 8},
{id: 9, attr: 3},
{id: 4, attr: null}
];
var temp = [];
for(var i = x.length-1;i>=0;i--){
if (x[i].attr===null) { //if we have a null
temp.unshift(x.splice(i,1)[0]); //remove the element from org and add to temp
}
}
x = temp.concat(x); //add the original to the temp to maintain the order

var arr = [
{id: 5, attr: 99},
{id: 7, attr: null},
{id: 2, attr: 8},
{id: 9, attr: 3},
{id: 4, attr: null}
]
var nulls = arr.filter(function(a){return a.attr === null});
var notnulls = arr.filter(function(a){return a.attr !== null});
nulls.concat(notnulls);
// outputs [{"id":7,"attr":null},{"id":4,"attr":null},{"id":5,"attr":99},{"id":2,"attr":8},{"id":9,"attr":3}]
And here's a solution using reduce:
var sorted = arr.reduce(function(output, element) {
if (element.attr === null)
output.unshift(element);
else
output.push(element);
return output
}, []);
console.log(sorted);

Grab the objects where attr === null.
var withNull = arr.filter(function (el) { return el.attr === null });
Grab the objects where attr !== null.
var withVal = arr.filter(function (el) { return el.attr !== null });
Add the 'null array' to the front of the 'values array'.
withVal.unshift.apply(withVal, withNull);
Fiddle

function customSort(myArray) {
var result = [];
myArray.map(function(el, idx, arr) {
return myArray[idx].attr === null ? result.unshift(myArray[idx]) : result.push(myArray[idx]);
});
return result;
}
console.log(customSort([
{id: 5, attr: 99},
{id: 7, attr: null},
{id: 2, attr: 8},
{id: 9, attr: 3},
{id: 4, attr: null}
]));

Related

Comparing Two Arrays with Array.filter and Pushing to New Arrays Based on Common Values

I am trying to use array.filter() to compare two arrays and separate out values that the two arrays have in common, based on a certain property (id), vs. values they don't have in common. The common ids I want to push to a new array (recordsToUpdate). And I want to push the remaining elements from arr2 to a new array (recordsToInsert).
What I've tried is not working. How can I rework this to get the results I wanted? - (which in the example here should be one array of 1 common element {id: 3}, and another array of the remaining elements from arr2):
const arr1 = [{id: 1}, {id: 2}, {id: 3}];
const arr2 = [{id: 3}, {id: 4}, {id: 5}];
let recordsToUpdate = [];
let recordsToInsert = [];
recordsToUpdate = arr1.filter(e => (arr1.id === arr2.id));
recordsToInsert = ?
console.log('recordsToUpdate: ', recordsToUpdate);
console.log('recordsToInsert: ', recordsToInsert);
The desired result should be:
recordsToUpdate = [{id: 3}];
recordsToInsert = [{id: 4}, {id: 5}];
Try this, which uses Array.prototype.find to test for whether an object exists in arr2 with a given id:
const arr1 = [{id: 1}, {id: 2}, {id: 3}];
const arr2 = [{id: 3}, {id: 4}, {id: 5}];
const recordsToUpdate = arr1.filter(e => arr2.find(obj => obj.id === e.id) !== undefined);
const recordsToInsert = arr1.filter(e => arr2.find(obj => obj.id === e.id) === undefined);
console.log('recordsToUpdate: ', recordsToUpdate);
console.log('recordsToInsert: ', recordsToInsert);
Update to Robin post using some instead of find. It is just other way around.
const arr1 = [{id: 1}, {id: 2}, {id: 3}];
const arr2 = [{id: 3}, {id: 4}, {id: 5}];
const recordsToUpdate = arr1.filter(e => arr2.some(obj => obj.id === e.id));
const recordsToInsert = arr2.filter(e => !arr1.some(obj => obj.id === e.id));
console.log('recordsToUpdate: ', recordsToUpdate);
console.log('recordsToInsert: ', recordsToInsert);
I think this is what you are after... I added values to show the replacement. If you are doing any kind of state management, be careful as I am directly mutating the current array.
const arr1 = [
{ id: 1, v: "a" },
{ id: 2, v: "b" },
{ id: 3, v: "old" }
];
const arr2 = [
{ id: 3, v: "new" },
{ id: 4, v: "e" },
{ id: 5, v: "f" }
];
function updateRecords(currentArray, updatesArray) {
const currentIds = currentArray.map(item => item.id);
updatesArray.forEach(updateItem =>
currentIds.includes(updateItem.id)
? (currentArray[
currentIds.findIndex(id => id === updateItem.id)
] = updateItem)
: currentArray.push(updateItem)
);
return currentArray;
}
console.log(updateRecords(arr1, arr2))
This now gives the option below:
[
{
"id": 1,
"v": "a"
},
{
"id": 2,
"v": "b"
},
{
"id": 3,
"v": "new"
},
{
"id": 4,
"v": "e"
},
{
"id": 5,
"v": "f"
}
]
Putting it in a function is also something you likely want to do as you will likely use this multiple places in your code.

Original array changed without any modifications done

I am trying to remove duplicate objects from an array, and keep only the objects which have the highest nb value.
Example:
From this array:
let arr = [
{id: 1, nb: 1},
{id: 1, nb: 4},
{id: 2, nb: 1},
{id: 3, nb: 1},
{id: 1, nb: 2},
{id: 1, nb: 3},
{id: 2, nb: 7},
{id: 2, nb: 8},
];
I am supposed to get this:
arr2 = [
{ id: 1, nb: 4 },
{ id: 2, nb: 8 },
{ id: 3, nb: 1 }
]
The algorithm below is very correct in theory, however I see the original array is modified by the end (see the last console.log(arr) below):
Code:
let arr = [
{id: 1, nb: 1},
{id: 1, nb: 4},
{id: 2, nb: 1},
{id: 3, nb: 1},
{id: 1, nb: 2},
{id: 1, nb: 3},
{id: 2, nb: 7},
{id: 2, nb: 8},
];
// Original array
console.log(arr);
let tmp = {};
for(let i=0; i<arr.length; i++) {
if( !tmp[arr[i].id] ) {
tmp[arr[i].id] = arr[i];
} else {
if (tmp[arr[i].id].nb < arr[i].nb ) {
tmp[arr[i].id].nb = arr[i].nb;
}
}
}
var result = Object.values(tmp);
// This output the desired result
console.log(result);
// Why the original array changed ?
console.log(arr);
This will output:
> Array [Object { id: 1, nb: 1 }, Object { id: 1, nb: 4 }, Object { id: 2, nb: 1 }, Object { id: 3, nb: 1 }, Object { id: 1, nb: 2 }, Object { id: 1, nb: 3 }, Object { id: 2, nb: 7 }, Object { id: 2, nb: 8 }]
> Array [Object { id: 1, nb: 4 }, Object { id: 2, nb: 8 }, Object { id: 3, nb: 1 }]
> Array [Object { id: 1, nb: 4 }, Object { id: 1, nb: 4 }, Object { id: 2, nb: 8 }, Object { id: 3, nb: 1 }, Object { id: 1, nb: 2 }, Object { id: 1, nb: 3 }, Object { id: 2, nb: 7 }, Object { id: 2, nb: 8 }]
Why did the original array changed when there is no processing on it apart from looping?
The original array is updated at last as the objects in your tmp map and arr share the same object reference. So changes made in tmp will be reflected in arr. You can use Object.assign() to make them point to separate reference. Try the following:
let arr = [ {id: 1, nb: 1}, {id: 1, nb: 4}, {id: 2, nb: 1}, {id: 3, nb: 1}, {id: 1, nb: 2}, {id: 1, nb: 3}, {id: 2, nb: 7}, {id: 2, nb: 8}, ];
let tmp = {};
for(let i=0; i<arr.length; i++) {
if( !tmp[arr[i].id] ) {
tmp[arr[i].id] = Object.assign({},arr[i]);
} else {
if (tmp[arr[i].id].nb < arr[i].nb ) {
tmp[arr[i].id].nb = arr[i].nb;
}
}
}
var result = Object.values(tmp);
console.log(result)
Because objects in both the arrays are sharing the same reference.
You will need to update from
tmp[arr[i].id] = arr[i];
to
tmp[arr[i].id] = JSON.parse(JSON.stringify(arr[i]));
let arr = [
{id: 1, nb: 1},
{id: 1, nb: 4},
{id: 2, nb: 1},
{id: 3, nb: 1},
{id: 1, nb: 2},
{id: 1, nb: 3},
{id: 2, nb: 7},
{id: 2, nb: 8},
];
let tmp = {};
for(let i=0; i<arr.length; i++) {
if( !tmp[arr[i].id] ) {
tmp[arr[i].id] = JSON.parse(JSON.stringify(arr[i]));
} else {
if (tmp[arr[i].id].nb < arr[i].nb ) {
tmp[arr[i].id].nb = arr[i].nb;
}
}
}
var result = Object.values(tmp);
console.log(arr); // original array unchanged

Merging of two arrays with conditions without using lodash

I am facing problem to merge the two arrays. I have two arrays of objects first is prev having old values and another with updated values. I would like to have result array with all the objects of prev array with its updated value in array next, and also have objects in next array.
Example:
var prev = [{id: 1, val: 'abc'}, {id: 2, val: 'pqr'}];
var next = [{id: 1, val: 'nextVal'}, {id: 3, val: 'xyz'}];
expected
mergeOutput = [
{id: 1, val: 'nextVal'}, // value is updated
{id: 2, val: 'pqr'},
{id: 3, val: 'xyz'}
]
Note: Array order do not matter.
You can use Map() to merge array.
var prev = [{id: 1, val: 'abc'}, {id: 2, val: 'pqr'}];
var next = [{id: 1, val: 'nextVal'}, {id: 3, val: 'xyz'}];
var hash = new Map();
prev.concat(next).forEach(function(obj) {
hash.set(obj.id, Object.assign(hash.get(obj.id) || {}, obj))
});
var mergedArray = Array.from(hash.values());
console.log(mergedArray);
Source : StackOverflow

Is there any lodash function to achieve this?

I have an array
var a = [
{id: 1, item: 3},
{id: 1, item: 4},
{id: 1, item: 5},
{id: 2, item: 6},
{id: 2, item: 7},
{id: 3, item: 8}
]
I need output like this:
[{id: 1, items: [3, 4, 5]}, {id: 2, items: [6,7]}, {id: 3, items: [8]}]
Here's a solution that first groups by id and then maps across the groupings to get the required collection:
let result = _(a)
.groupBy('id')
.map( (group ,id) => ({id: id, items: _.map(group, 'item')}))
.value()
It's pretty ugly, but then other answers are not pretty either
var a = [
{id: 1, item: 3},
{id: 1, item: 4},
{id: 1, item: 5},
{id: 2, item: 6},
{id: 2, item: 7},
{id: 3, item: 8}
];
var ret = _.chain(a)
.groupBy(elt => elt.id)
.mapValues(elt => _.reduce(elt, (acc, sub) => acc.concat(sub.item),[]))
.map((value, key) => ({id: key, items:value}))
.value();
console.log(ret);
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>

Compare the elements of two arrays by Id and remove the elements from the one array that are not presented in the other

I have two arrays of objects like this:
var arr1 = [{Id: 1, Name: "Test1"}, {Id: 2, Name: "Test2"}, {Id: 3, Name: "Test3"}, {Id: 4, Name: "Test4"}]
var arr2 = [{Id: 1, Name: "Test1"}, {Id: 3, Name: "Test3"}]
I need to compare the elements of the two arrays by Id and remove the elements from arr1 that are not presented in arr2 ( does not have element with that Id). How can I do this ?
var res = arr1.filter(function(o) {
return arr2.some(function(o2) {
return o.Id === o2.Id;
})
});
shim, shim, shim.
You can use a function that accepts any number of arrays, and returns only the items that are present in all of them.
function compare() {
let arr = [...arguments];
return arr.shift().filter( y =>
arr.every( x => x.some( j => j.Id === y.Id) )
)
}
var arr1 = [{Id: 1, Name: "Test1"}, {Id: 2, Name: "Test2"}, {Id: 3, Name: "Test3"}, {Id: 4, Name: "Test4"}];
var arr2 = [{Id: 1, Name: "Test1"}, {Id: 3, Name: "Test3"}, {Id: 30, Name: "Test3"}];
var arr3 = [{Id: 1, Name: "Test1"}, {Id: 6, Name: "Test3"}, {Id: 30, Name: "Test3"}];
var new_arr = compare(arr1, arr2, arr3);
console.log(new_arr);
function compare() {
let arr = [...arguments]
return arr.shift().filter( y =>
arr.every( x => x.some( j => j.Id === y.Id) )
)
}
Making use of a hash (a Set) will give a performance gain:
var arr1 = [{Id: 1, Name: "Test1"}, {Id: 2, Name: "Test2"},
{Id: 3, Name: "Test3"}, {Id: 4, Name: "Test4"}];
var arr2 = [{Id: 1, Name: "Test1"}, {Id: 3, Name: "Test3"}];
arr1 = arr1.filter(function (el) {
return this.has(el.Id);
}, new Set(arr2.map(el => el.Id)));
console.log(arr1);
A new Set is created that gets the Id values from arr2:
"1","3"
That Set is passed as the thisArg to filter, so that within the filter callback it is available as this.

Categories

Resources