Compare two Arrays and Insert Null Vallues

Viewed 105

I have two arrays:

var array1 = [{id: 1, time: 100}, {id: 2, time: 200}, {id: 3, time: 300}];
var array2 = [{id: 1, time: 100}, {id: 3, time: 300}];

And I would like for array2 to be changed to

var array2 = [{id: 1, time: 100}, null, {id: 3, time: 300}];

The question is how can I compare the two arrays and look at their time and then insert null in the missing locations for each array.

Any help is appreciated!

4 Answers

const arr1 = [{id: 1, time: 100}, {id: 2, time: 200}, {id: 3, time: 300}];
const arr2 = [{id: 1, time: 100}, {id: 3, time: 300}, {id: 3, time: 400}];

const uniqueTimes = [...arr1, ...arr2]
  .filter((e, i, a) => a.findIndex(x => x.time === e.time) === i)

const res1 = uniqueTimes.map(e => 
  arr1.find(x => x.time === e.time) ?? null
)

const res2 = uniqueTimes.map(e => 
  arr2.find(x => x.time === e.time) ?? null
)

console.log(res1)
console.log(res2)

Here's one way to do it.

var array1 = [{
  id: 1,
  time: 100
}, {
  id: 2,
  time: 200
}, {
  id: 3,
  time: 300
}];
var array2 = [{
  id: 1,
  time: 100
}, {
  id: 3,
  time: 300
}];

const fixArray = (a, maxTime) => {
  let inc = 100,
    start = inc,
    tmp = [];
  // first make sure we have it in order
  //a = a.sort((a, b) => (a.time < b.time) ? -1 : 1)
  while (start < maxTime) {
    let t = a.filter(el => el.time === start)
    if (t.length === 0) tmp.push(null);
    else tmp.push(t[0])
    start += inc;
  }
  return tmp
}

array2 = fixArray(array2, 1000);
console.log(array2)

Your example is a little misleading. Your description of the prompt says entries can be missing in both arrays, right? My example has 200 missing in array2, and 400 missing in array1

var array1 = [{ id: 1, time: 100 }, { id: 2, time: 200 }, { id: 3, time: 300 }];
var array2 = [{ id: 1, time: 100 }, { id: 3, time: 300 }, { id: 1, time: 400 }];
// get all possible times, sort them
const allSortedTimes = array1.map(({ time }) => time).concat(array2.map(({ time }) => time)).sort((a, b) => a - b)
// only use uniq times
const allUniqTimes = [...new Set(allSortedTimes)]
// now that we have all the possible times,
// we go over each array and check to see if that time exists
const insertedArray1 = allUniqTimes.map((uniqTime) => {
  return array1.find(({ time }) => time === uniqTime) ?? null
})

const insertedArray2 = allUniqTimes.map((uniqTime) => {
  return array2.find(({time}) => time === uniqTime) ?? null
})
console.log(insertedArray1)
console.log(insertedArray2)

const getKey=(id,time)=>id+"_"+time //Provides unique key based on object values


var array1 = [{id: 1, time: 100}, {id: 2, time: 200}, {id: 3, time: 300}];
var array2 = [{id: 0, time: 5},{id: 1, time: 100},{id: 11, time: 250}, {id: 3, time: 300},{id: 5, time: 500}];

let keysOfArray1=[]//array1 unique keys
let keysOfArray2=[]//array2 unique keys

array1.map(item=>{
    keysOfArray1.push(getKey(item.id,item.time)); // collects array1 unique keys
    return item
}).concat(array2.map(item=>{//concat array2 values with array1
    keysOfArray2.push(getKey(item.id,item.time)) // collects array2 unique keys
    return item
})).sort((a,b)=>a.time-b.time || a.id-b.id).reduce((prev,current)=>{ //Sort by time & then Id
    let keyName=getKey(current.id,current.time)
    if(!prev.includes(keyName)){// To consider all objects only once
        array1[prev.length]=keysOfArray1.includes(keyName)?current:null
        array2[prev.length]=keysOfArray2.includes(keyName)?current:null
        prev.push(keyName)
    }
    return prev
},[])


console.log(array1);
console.log(array2);

Related