Combine Arrays in JavaScript Without Duplicates [duplicate] - javascript

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 5 months ago.
I need to combine ccAddresses and toAddresses values into just one set of array without duplication.
Expected Output
{ "newAddresses": ["john#gmail.com", "joshua#gmail.com", "jane#gmail.com", "james#gmail.com"] }
const old = [
{
"ccAddresses": ["john#gmail.com", "joshua#gmail.com"],
},
{
"ccAddresses": ["jane#gmail.com", "joshua#gmail.com"],
"toAddresses": ["jane#gmail.com", "james#gmail.com"],
}
];
const news = old.flatMap((val, index) => ({
"newAddress": val.ccAddresses
}))
console.log(news)

const res = Array.from(new Set([...old.flatMap(x => [...x.ccAddresses])]))
UPDATED(as per new author's requirement):
Array.from(new Set([...old.flatMap(x => [...x.ccAddresses].concat([...x.toAddresses || []]))]))

Related

Create a array of objects by grouping objects conditionally [duplicate]

This question already has answers here:
Group array items using object
(19 answers)
How can I group an array of objects by key?
(32 answers)
Closed last month.
I am trying to create a new array of objects by comparing two arrays.One array contains the ids to be compared and the other contains the dataset which should be compared with the first array are a new array of object must be created.
Let me explain this in detail
Consider array 1:
['1','2']
Array 2
[{name:'Linus',id:'1'},{name:'Anthony',id:'1'},{name:'Jake',id:'2'},{name:'Eva',id:'2'}]
What I am expecting as a output is:
[
{id:'1',users:[{name:'Linus',id:'1'},{name:'Anthony',id:'1'}]},
{id:'2',users:[{name:'Jake',id:'2'},{name:'Eva',id:'2'}
]
I am not sure what has to be done.
Something like this for example
const ids = ['1','2'];
const users = [{name:Linus,id:'1'},{name:Anthony,id:'1'},{name:Jake,id:'2'},{name:Eva,id:'2'}];
const groups = ids.map(id =>{
return {
id,
users: users.filter(u => u.id === id)
}
})
const groupBy = ['1','2'];
const data = [{name:'Linus',id:'1'},{name:'Anthony',id:'1'},{name:'Jake',id:'2'},{name:'Eva',id:'2'}];
const result = groupBy.map(id => {
return {
id,
users: data.filter(item => item.id == id),
}
})
console.log(result);
Try this code

How do you group an array via JavaScript [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 4 months ago.
How do I group the array via javascript to just
Vehicles
Food
I can't figure it out via "reduce" since the array has no "key" to specify.
Thank you
const group1 = arr.filter(item => item === 'Vehicles');
const group2 = arr.filter(item => item === 'Food');
Or if you want a general solution:
let groups = {};
for (const item of arr) {
if (!groups[item]) groups[item] = [];
groups[item].push(item);
}
I think I don't get your question completely, but if you are trying to remove duplicates, this could be an option
const originalArray = ["vehicle", "food", "vehicle", "food", "vehicle", "food"];
const noDuplicates = new Set(originalArray);
console.log(Array.from(noDuplicates));

JavaScript array manipulation interview question [duplicate]

This question already has answers here:
Merge objects with same id in array
(7 answers)
Closed 1 year ago.
This is a question that I have encountered during an interview:
Write a function to transform the array:
[
{name:'a',values:[1,2]},
{name:'b',values:[3]},
{name:'a',values:[4,5]}
]
to:
[
{name:'a',values:[1,2,4,5]},
{name:'b',values:[3]}
]
I know this is not hard, but I just can't figure it out.
Does anyone know how to solve it?
Are there any places that I can find and practice more practical questions like this one?
You can group the array by name and then get an array using the grouped object:
const bla = [
{name:'a',values:[1,2]},
{name:'b',values:[3]},
{name:'a',values:[4,5]}
];
const res = Object.values(bla.reduce((obj, { name, values }) => {
obj[name] = obj[name] ?? {name, values: []}
obj[name].values.push(...values)
return obj
}, {}));
console.log(res)

how can i simplify this return statement? [duplicate]

This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 3 years ago.
is it possible simplify .map's return statement? I want to add key for each element in the array, and transform arr to array of object. thanks!
let arr = ['c:/a.txt', 'b:/c.txt', 'd:/e.txt'];
let arrObj = arr.map((file) => return {path: file});
You could use a short hand property.
let arr = ['c:/a.txt', 'b:/c.txt', 'd:/e.txt'],
arrObj = arr.map(path => ({ path }));
console.log(arrObj);

Javascript to Group Array of Object into combination of two values [duplicate]

This question already has answers here:
How can I group an array of objects by key?
(32 answers)
Create Multilevel groups from an Array of Objects
(2 answers)
Closed 4 years ago.
var data=[
{custType:1,code:'savings',efDate:'2/3/19',exDate'2/3/19'},
{custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:1,code:'current',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'current',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'}
];
expected results
var modifiedData=[
[
savings=[ {custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'}
],
current=[ {custType:1,code:'current',efDate:'2/3/19',exDate:'2/3/19'} ]
],
[
savings=[ {custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'} ],
current=[ {custType:2,code:'current',efDate:'2/3/19',exDate:'2/3/19'} ]
]
];
as i am trying to find a group with (custType and code), i am not able to generate the expected result. Which method i should use here? can i use reduce or groupBy. I am confused about it.
You could build up a nested hashtable:
const hash = {};
for(const value of data) {
const group = hash[value.custType] || (hash[value.custType] = {});
const group2 = group[value.code] || (group[value.code] = []);
group2.push(value);
}
const result = Object.values(hash).map(Object.values);

Categories

Resources