How to union to immutable.js list of objects (with id prop)
up vote
0
down vote
favorite
I want to replace / add new values in an immutable list like:
const old = [
id: 1, value: 'foo' ,
id: 3, value: 'bar' ,
id: 2, value: 'baz' ,
]
const new = [
id: 1, value: 'quux' , // update
id: 4, value: 'asd' , // push
]
union == [
id: 1, value: 'quux' , // updated
id: 3, value: 'bar' ,
id: 2, value: 'baz' ,
id: 4, value: 'asd' , // new
]
I can use any data type if lists don't have this functionality but something has
If this isn't built in immutable.js just say that, i already wrote a script for this, i'm just missing the built in method
javascript immutable.js
add a comment |
up vote
0
down vote
favorite
I want to replace / add new values in an immutable list like:
const old = [
id: 1, value: 'foo' ,
id: 3, value: 'bar' ,
id: 2, value: 'baz' ,
]
const new = [
id: 1, value: 'quux' , // update
id: 4, value: 'asd' , // push
]
union == [
id: 1, value: 'quux' , // updated
id: 3, value: 'bar' ,
id: 2, value: 'baz' ,
id: 4, value: 'asd' , // new
]
I can use any data type if lists don't have this functionality but something has
If this isn't built in immutable.js just say that, i already wrote a script for this, i'm just missing the built in method
javascript immutable.js
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to replace / add new values in an immutable list like:
const old = [
id: 1, value: 'foo' ,
id: 3, value: 'bar' ,
id: 2, value: 'baz' ,
]
const new = [
id: 1, value: 'quux' , // update
id: 4, value: 'asd' , // push
]
union == [
id: 1, value: 'quux' , // updated
id: 3, value: 'bar' ,
id: 2, value: 'baz' ,
id: 4, value: 'asd' , // new
]
I can use any data type if lists don't have this functionality but something has
If this isn't built in immutable.js just say that, i already wrote a script for this, i'm just missing the built in method
javascript immutable.js
I want to replace / add new values in an immutable list like:
const old = [
id: 1, value: 'foo' ,
id: 3, value: 'bar' ,
id: 2, value: 'baz' ,
]
const new = [
id: 1, value: 'quux' , // update
id: 4, value: 'asd' , // push
]
union == [
id: 1, value: 'quux' , // updated
id: 3, value: 'bar' ,
id: 2, value: 'baz' ,
id: 4, value: 'asd' , // new
]
I can use any data type if lists don't have this functionality but something has
If this isn't built in immutable.js just say that, i already wrote a script for this, i'm just missing the built in method
javascript immutable.js
javascript immutable.js
asked Nov 10 at 13:14
tomitheninja
63
63
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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/
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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/
add a comment |
up vote
0
down vote
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/
add a comment |
up vote
0
down vote
up vote
0
down vote
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/
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/
answered Nov 10 at 14:06
front_end_dev
1,310411
1,310411
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239305%2fhow-to-union-to-immutable-js-list-of-objects-with-id-prop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown