0

have 2 arrays: look like this

1) first array

{day:mo, hours:[{id:1,color:grey},{id:2,color:grey},{id:3,color:grey},{id:4,color:grey}]} 

2) second array

{day:mo, hours:[{id:1,color:white},{id:2,color:white},{id:3,color:white},{id:4,color:white},{id:5,color:white}]} 

result must be

{day:mo, hours:[{id:1,color:grey},{id:2,color:grey},{id:3,color:grey},{id:4,color:grey},{id:5,color:white}]} 

SO I need to delete from 2 array same values like in first, and then concat it.

try do something this

clearArrFunc(arr1,arr2) { // do something for(var i in arr1){ console.log(arr1[i]); for(var j in arr2) { if(JSON.stringify(arr1[i].day) == JSON.stringify(arr2[j].day)) { delete arr2[j] ; } } } // console.log(arr2); return arr1.concat(arr2); } 
2
  • 1
    Those are not array, those are objects Commented Aug 24, 2017 at 12:40
  • hours is array with objects Commented Aug 24, 2017 at 12:41

3 Answers 3

2

Concat both arrays and then eliminate duplicate ids from the result:

a = [{id:1,color:'grey'},{id:2,color:'grey'},{id:3,color:'grey'},{id:4,color:'grey'}] b = [{id:1,color:'white'},{id:2,color:'white'},{id:3,color:'white'},{id:4,color:'white'},{id:5,color:'white'}] result = a .concat(b) .filter((x, i, self) => self.findIndex(y => y.id === x.id) === i); console.log(result)

A less concise but more efficient option is to build a Set of ids from the first array and then only append elements from the 2nd whose ids are not in the set:

a = [{id:1,color:'grey'},{id:2,color:'grey'},{id:3,color:'grey'},{id:4,color:'grey'}] b = [{id:1,color:'white'},{id:2,color:'white'},{id:3,color:'white'},{id:4,color:'white'},{id:5,color:'white'}] ids = new Set(a.map(x => x.id)) result = a.concat(b.filter(x => !ids.has(x.id))) console.log(result)

Sign up to request clarification or add additional context in comments.

2 Comments

My object have monday. tuesday wednesday, so need compare if first array have monday, delete values from 2 array. if Not add monday from second array
both snippets schould work just fine if one of the arrays is empty
0

Another approach, remove those values from 2nd array which are present in 1st and concat the result with first array.

var x = [{id:1,color:'grey'},{id:2,color:'grey'},{id:3,color:'grey'},{id:4,color:'grey'}]; var y = [{id:1,color:'white'},{id:2,color:'white'},{id:3,color:'white'},{id:4,color:'white'},{id:5,color:'white'}]; var r = x.concat(y.filter(function(obj) { return !x.some(function(o) { return o.id===obj.id; }); })); console.log(r);

3 Comments

but first need compare by days. if first array have monday, delete from second array all ids that == second. and add ids from first
@ЕгорКротенко And if days are not same, what output do you want ?
I want remove from second array all hours == first array hours, and push there first array hours, if we don't have date, just push all date and hours from second array
0

Using Lodash (https://lodash.com)

const _ = require('lodash'); const a = [{id: 1, color: "grey"},{id: 2, color: "grey"},{id: 3, color: "grey"},{id: 4, color: "grey"}]; const b = [{id: 1, color: "white"},{id: 2, color: "white"},{id: 3, color: "white"},{id: 4, color: "white"},{id: 5, color: "white" }]; const result = _.unionBy(o1.hours, o2.hours), 'id'); // result = [{id: 1, color: "grey"}, {id: 2, color: "grey"}, {id: 3, color: "grey"}, {id: 4, color: "grey"}, {id: 5, color: "white"}] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.