I have two list of objects:
list1 = [{value: 'X'}, {value: 'Y'}, ..., {value: 'Z'}];
list2 = [{value: 'A'}, {value: 'B'}, ..., {value: 'C'}];
I have this code that checks whether the values in list2 are in list1. If it is the code doesn't do anything, if not it should add to list1 (this will create a new list, list3). Which means I'm doing a union between the two list without keeping the repeated values.
for (let i = list2.length-1; i >= 0; i--) {
let item = list2[i];
let shared = false;
for (let j = list1.length-1; j >=0; j--) {
let childItem = list1[j];
if (item.value === childItem.value) {
shared = true;
break;
}
}
if (!shared) { newValues.push(item); }
}
list3 = list1.concat(newValues);
This works fine, but I was wondering if I could improve this O(n*m).
I'm not sure if the lists are always sorted by default, but from what I've seen both (list1 and list2) are always sorted by value.
Example:
var list1 = [{value: 'bar'}, {value: 'baz'}, {value: 'foo'}, {value: 'foz'}];
var list2 = [{value: 'bar'}, {value: 'foo'}, {value: 'test'}, {value: 'testz'}];
var list3 = union(list1, list2);
list3 = [{value: 'bar'}, {value: 'baz'}, {value: 'foo'}, {value: 'foz'}, {value: 'test'}, {value: 'testz'}];
Create a set of the values of list1, and Filter list2 by the values in the set before concating it to list1:
var list1 = [{value: 'bar'}, {value: 'baz'}, {value: 'foo'}, {value: 'foz'}];
var list2 = [{value: 'bar'}, {value: 'foo'}, {value: 'test'}, {value: 'testz'}];
const union = (list1, list2) => list1.concat(
list2.filter(function({ value }) { // filter list2
return !this.has(value); // filter out items which value is in the set
}, new Set(list1.map(({ value }) => value))) // the set of list1 values
);
const list3 = union(list1, list2);
console.log(list3);
You could use a single loop and store the value in a set for later check. Complexity: O(n).
var list1 = [{ value: 'X' }, { value: 'Y' }, { value: 'C' }, { value: 'Z' }],
list2 = [{ value: 'A' }, { value: 'B' }, { value: 'C' }, { value: 'D' }],
list3 = list1
.concat(list2)
.filter((s => ({ value }) => !s.has(value) && s.add(value))(new Set));
console.log(list3);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related
I have 2 arrays and i'd like to filter one array with the other. E.g. if array1 includes any of the values in array2, they should be returned.
The two arrays are:
const array1 = [a, b, c, d]
The other array, which should be filtered where 'id' is equal to any of the values in array1 is:
const array2 = [
{
id: b
title: title1
},
{
id: d
title: title2
},
{
id: f
title: title3
}
]
The easiest way is to use two for-loops. Possible not the fastest approach.
res = [];
for (var i = 0;i<array1.length;i++) {
for (var j = 0;j<array2.length;j++) {
if (array1[i] == array2[j].id) {
res.push(array2[j]);
break;
}
}
}
You could use Array.prototype.filter() and Array.prototype.indexOf():
const array1 = ['a', 'b', 'c', 'd'];
const array2 = [{
id: 'b',
title: 'title1'
}, {
id: 'd',
title: 'title2'
}, {
id: 'f',
title: 'title3'
}];
const result = array2.filter(function(x){
return array1.indexOf(x.id) !== -1;
});
Adding this missing '', You can use filter and includes methods of Array.
const array1 = ['a', 'b', 'c', 'd'];
const array2 = [
{
id: 'b',
title: 'title1'
},
{
id: 'd',
title: 'title2'
},
{
id: 'f',
title: 'title3'
}
]
const result = array2.filter(({id}) => array1.includes(id));
console.log(result);
I want to dynamically create an object with key value pairs from an array of object. How can this be done?
const arr= [
{key: 'a', value: '1'},
{key: 'b', value: '2'},
{key: 'c', value: '3'},
];
The result should look like this:
const obj = {
a: '1',
b: '2',
c: '3',
};
I would use reduce function for that.
The agg is an aggregator which aggregate our final result.
The item is representing each element in the array.
const arr= [
{key: 'a', value: '1'},
{key: 'b', value: '2'},
{key: 'c', value: '3'},
];
const result = arr.reduce((agg, item) => {
agg[item.key] = item.value
return agg
}, {})
console.log(result)
// { a: '1', b: '2', c: '3' }
The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.
simple forEach will do the job
const arr= [
{key: 'a', value: '1'},
{key: 'b', value: '2'},
{key: 'c', value: '3'},
];
const obj = {};
arr.forEach(v=>{
obj[v.key] = v.value;
});
console.log(obj);
Very easy, probably a duplicate.
const arr= [
{key: 'a', value: '1'},
{key: 'b', value: '2'},
{key: 'c', value: '3'},
];
const obj = {};
for (let i = 0; i < arr.length; ++i) {
obj[arr[i].key] = arr[i].value
}
console.log(obj);
function parse(input){
return input.reduce(function(o,i){
o[i.key] = i.value;
return o;
},{});
}
Use Array.forEach() and the square brackets syntax to set the properties of the new object.
const arr = [
{key: 'a', value: '1'},
{key: 'b', value: '2'},
{key: 'c', value: '3'},
];
const obj = {};
arr.forEach(({key, value}) => obj[key] = value);
console.log(obj);
You can simply create an empty object and then create new properties on the fly, while iterating an array.
let obj = {};
for(let i = 0; i < arr.length; i++) {
obj[arr[i].key] = obj[i].value;
}
This is my Code. Where I want to Pass the Values of kvArray to Second Array.
var kvArray = [{key: 1, value: 10},
{key: 2, value: 20},
{key: 3, value: 30}];
var reformattedArray = kvArray.map(obj => {
var payload = {};
payload["rt"];
payload["do"];
payload["f1"];
payload[obj.key] = obj.value;
console.log(payload["rt"]);
return payload;
});
The console.log is coming undefined.
Can anyone help here? I am pretty new to Map function.
I want to Print this result.
payload["do"]=10
payload["f1"]=20
payload["f2"]=30
var kvArray = [{key: 1, value: 10},
{key: 2, value: 20},
{key: 3, value: 30}];
var reformattedArray = kvArray.map(obj =>{
var payload = {};
const mapping = [null, 'rt', 'do', 'f1'];
const key = mapping[obj.key];
payload[key] = obj.value;
return payload;
});
console.log(reformattedArray);
You could use a destructuring assignment and build a new object with computed property names.
For the final keys, you could use an object keys with corresopnding keys to the the keys of the given object.
var kvArray = [{ key: 1, value: 10 }, { key: 2, value: 20 }, { key: 3, value: 30 }],
keys = { 1: 'rt', 2: 'do', 3: 'fi' },
result = kvArray.map(({ key, value }) => ({ [keys[key]]: value }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I'm not sure what format you want.
try the code below:
var kvArray = [{ key: 1, value: 10 },
{ key: 2, value: 20 },
{ key: 3, value: 30 }];
var reformattedArray = kvArray.map(obj => obj.value);
console.log(reformattedArray)
the result will be:
[ 10, 20, 30 ]
Not sure what you are trying to achieve with lines:
payload["rt"];
payload["do"];
payload["f1"];
If you want to create new keys in the reformatterArray, try assigning a value, eg.
var kvArray = [{key: 1, value: 10},
{key: 2, value: 20},
{key: 3, value: 30}];
var reformattedArray = kvArray.map(obj =>{
var payload = {};
payload["rt"] = "";
payload["do"]= "";
payload["f1"]= "";
payload[obj.key] = obj.value;
console.log(payload["rt"]);
return payload;
});
console.log(reformattedArray):
//result
0: {1: 10, rt: "", do: "", f1: ""}
1: {2: 20, rt: "", do: "", f1: ""}
2: {3: 30, rt: "", do: "", f1: ""}
You were assigning the value to payload but getting the values of payload.
var kvArray = [{key: 1, value: 10},
{key: 2, value: 20},
{key: 3, value: 30}];
var reformattedArray = kvArray.map(obj =>{
var payload = {};
payload[obj.key] = obj.value;
console.log(payload[obj.key]);
return payload;
});
You are mapping data to reformattedArray , so you need to pring reformattedArray to get values,
var kvArray = [{key: 1, value: 10},
{key: 2, value: 20},
{key: 3, value: 30}];
var reformattedArray = kvArray.map(obj =>{
var payload = {};
payload[obj.key] = obj.value;
return payload;
});
console.log(reformattedArray);
Also you have following code which is of no use
payload["rt"];
payload["do"];
payload["f1"];
This code works fine
let kvArray = [{key: 1, value: 10},
{key: 2, value: 20},
{key: 3, value: 30}];
let reformattedArray = kvArray.map(obj =>{
var payload = {};
payload[obj.key] = obj.value;
return payload;
});
console.log(reformattedArray)
The above code print output like this
[
0: {1: 10}
1: {2: 20}
2: {3: 30}
]
For more info, refer this link
I have 4 arrays of the following format
arr1 = ['Hello', 'World', 'Hi']
arr2 = ['1', '2', '3']
arr3 = ['foo', 'bar', 'foobar']
arr4 = ['10', '20', '30']
I am trying to add each value at index[i] to a new object, the object looks like this
obj = {
title: '',
score: '',
description: '',
value: '',
}
For each indexed value in the array I would like to push it to a new instance of the obj object so I can end up with this
objects = [
{
title: 'Hello',
score: '1',
description: 'foo',
value: '10',
},
{
title: 'World',
score: '2',
description: 'bar',
value: '20',
},
{
title: 'Hi',
score: '3',
description: 'foobar',
value: '30',
}
]
So far I have been trying something like
objects = []
arr1.forEach((key, i) => objects[key] = arr2[i])
But that is assigning them as arr1val : arr2val
I will ultimately be setting this to state in my react app and passing it to another component as props to render on the page. The data is coming in from 3 different APIs, I am doing this to try and standardise the data received from each API so my component can use the data to render an articles list and user can switch between feeds.
Map by the index of element across all arrays, also use map not forEach:
const objects = arr1.map((element, index) => (
{title: element, score: arr2[index], description: arr3[index], value: arr4[index]}
))
Live demo is below:
const arr1 = ['Hello', 'World', 'Hi']
const arr2 = ['1', '2', '3']
const arr3 = ['foo', 'bar', 'foobar']
const arr4 = ['10', '20', '30']
const objects = arr1.map((element, index) => (
{title: element, score: arr2[index], description: arr3[index], value: arr4[index]}
))
console.log(objects)
This code also works:
var arr = [];
for(var i=0; i<arr1.length; i++) {
arr[i] = {};
arr[i].title = arr1[i];
arr[i].score = arr2[i];
arr[i].description = arr3[i];
arr[i].value = arr4[i];
}
I suggest to use an array of arrays and another array for the keys. This allows an arbitrary count of arrays and keys to use for transforming the data into an array of objects with hte wanted properties.
var array1 = ['Hello', 'World', 'Hi'],
array2 = ['1', '2', '3'],
array3 = ['foo', 'bar', 'foobar'],
array4 = ['10', '20', '30'],
keys = ['title', 'score', 'description', 'value'],
result = [array1, array2, array3, array4].reduce(function (r, a, i) {
a.forEach(function (v, j) {
r[j] = r[j] || {};
r[j][keys[i]] = v;
});
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you don't want to type the property names twice,
you could do like this:
const descriptor = {
title: arr1,
score: arr2,
description: arr3,
value: arr4
};
const objects = arr1.map((tmp, i) => {
var o = {};
Object.keys(descriptor).forEach(name => o[name] = descriptor[name][i]);
return o;
});
A reducer method would be in place I suppose
const arr1 = ['Hello', 'World', 'Hi', 'Hello2', 'World2', 'Hi2'];
const arr2 = ['1', '2', '3', '11', '22', '33'];
const arr3 = ['foo', 'bar', 'foobar', 'sea', 'shell', 'sea weed'];
const arr4 = ['10', '20', '30', '100', '200', '300'];
const arrays2Objects = arr1.reduce( (obj, next, i) =>
obj.concat({title: next, score: arr2[i], description: arr3[i], value: arr4[i]}),
[]
);
console.log(arrays2Objects);
I have a stupid problem that at first seems to be simple to solve, but turns out to be tricky.
I have an array of objects, each with two properties: id and value:
[
{id: 2, value: 10},
{id: 4, value: 3},
{id: 2, value: 2},
{id: 1, value: 15}
]
I want to write an algorithm that sums up the values of ones with similar id.
My end result should be a new array with only the merged objects:
[
{id: 2, value: 12},
{id: 4, value: 3},
{id: 1, value: 15}
]
I've tried the following, but it doesn't work:
var arr = [];
arr.push({id: 2, visit:10});
arr.push({id: 4, visit:3});
arr.push({id: 2, visit:2});
arr.push({id: 1, visit:15});
// Deep copy
var copy = jQuery.extend(true, [], arr);
var masterArr = [];
for (var i = 0; i < arr.length; i++) {
var objArr = [];
objArr.push(arr[i]);
for (var j = copy.length-1; j > -1; j--) {
if (arr[i].id === copy[j].id) {
var q = copy.splice(j,1);
}
}
masterArr.push(objArr);
}
My plan was to first gather all similar objects in separate arrays (objArr), sum them up and put them in an end array (masterArr). I use jquerys extend to make a deep copy (not a reference) and reverse iteration and splice to remove objects thats already been found as "duplicates".
This doesn't work! And it doesn't seem to be a very efficient mehtod to solve my problem.
How could I do this? Performance isn't top priority but rather "nice to have"!
Thanks!
You can do it like this:
// Assuming:
a = [{id: 2, value: 10}, {id: 4, value: 3}, {id: 2, value: 2}, {id: 1, value: 15}]
var b = {}, // Temporary variable;
c = []; // This will contain the result;
// Build a id:value object ( {1: 15, 2: 12, 4: 3} )
a.map(function(current){b[current.id] = (b[current.id] || 0) + current.value});
for(var key in b){ // Form that into the desired output format.
c.push({id: parseInt(key, 10), value: b[key]});
}
console.log(c);
/* [{id: 1, value: 15},
{id: 2, value: 12},
{id: 4, value: 3}] */
I'm using parseInt(key, 10), since the keys are strings, you'll probably want them converted to integers again.
// First group the data based on id and sum the values
var temp = data.reduce(function(result, current) {
result[current.id] = (result[current.id] || 0) + current.value;
return result;
}, {});
// then recreate the objects with proper id and value properties
var result = [];
for (var key in temp) {
result.push({
id: parseInt(key, 10),
value: temp[key]
});
}
console.log(result);
Output
[ { id: 1, value: 15 },
{ id: 2, value: 12 },
{ id: 4, value: 3 } ]
The quickest approach loops over the array only once using Array.prototype.filter():
var tmp = {},
result = arr.filter(function (el) {
if (tmp.hasOwnProperty(el.id)) {
tmp[el.id].visit += el.visit;
return false;
}
else {
tmp[el.id] = el;
return true;
}
});
It also reuses the objects, though this renders the original array to contain inaccurate values. If this is a problem, you can modify the example to copy each object property to a new object.