tomitheninja
tomitheninja

Reputation: 537

Immutable.js algorithm: List.update_or_add(item)

I want to concatenate 2 lists in immutable.js.

Both lists have this structure: { id, value }

The algorithm concatenate should do this:

let list1 = [
    { id: 1, value: 'foo' },
    { id: 3, value: 'bar' },
    { id: 2, value: 'baz' },
]

let list2 = [
    { id: 1, value: 'quux' }, // id 1 exists in list1
    { id: 4, value: 'asd' },
]

let result = [
    { id: 1, value: 'quux' }, // from list 2 
    { id: 3, value: 'bar' },
    { id: 2, value: 'baz' },
    { id: 4, value: 'asd' },
]

If Immutable.js has this functionality with another type (eg. Dictionary), I could also use that.

Upvotes: 0

Views: 89

Answers (1)

front_end_dev
front_end_dev

Reputation: 2056

Algorithms for union

First you have to maintain two map with key as id and value as object then check for length of array which is of bigger size and pass the bigger size array with small size map to merged function there you can iterate over the array and check if it's exists in the map if yes then update the object and delete that row from map otherwise add the object into output. After the for loop complete check if map has element present then push all the values from map into output array and return;

index.js

const old = [
  { id: 1, value: 'foo' },
  { id: 3, value: 'bar' },
  { id: 2, value: 'baz' },
];

const newa = [
    { id: 1, value: 'quux' }, // update
    { id: 4, value: 'asd' }, // push

];

 function merged(input,filterMap){
     var output = [];
      input.forEach(function(eachRow){
                        if(filterMap.hasOwnProperty(eachRow.id)){
                 output.push(Object.assign(eachRow,filterMap[eachRow.id]));
                 delete filterMap[eachRow.id];
              }else{
                  output.push(eachRow);
              }
                });

          if(Object.keys(filterMap).length > 0){
            output = output.concat(Object.values(filterMap));
          }
          return output;
 }

function parseData(first,second){
   var mapFirst = {},
       mapSecond = {};
   var output = [];
   first.forEach(function(eachRow){
            mapFirst[eachRow.id] = eachRow;
        });

   second.forEach(function(eachRow){
                    mapSecond[eachRow.id] = eachRow;
   });

   if(first.length > second.length){
        return merged(first,mapSecond);
   }else{
     return merged(second,mapFirst);
   }
}

console.log(parseData(old,newa));

Working jsFiddle demo - https://jsfiddle.net/qz25hnmf/

Upvotes: 1

Related Questions