How to implement complex arrays? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
For example, when id is equal to 1, an array is ['a', 'b'], and when id is equal to 2, another array is ['c']. The result of these two arrays is [["a","b "],["c"]]
I want the results as follows:
[["a","b"],["c"]]
my code:
var data = [{
id: 1,
name: 'a'
},
{
id: 1,
name: 'b'
},
{
id: 2,
name: 'c'
}
]

Using reduce:
var data = [{
id: 1,
name: 'a'
},
{
id: 1,
name: 'b'
},
{
id: 2,
name: 'c'
}
]
let result = data.reduce((acc, item) => {
return item.id === 1 ? acc[0].push(item.name) : acc[1].push(item.name), acc
},[[], []])
console.log(result)

Related

How to copy key's value in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have 2 array
var arr = [{ 1: a }, { 2: b }, { 3: d }];
var arr1 = [{ 1: a }, { 2: c }, { 3: e }];
I want to copy arr1 key's value to arr. below I want output result
arr = [{ 1: a }, { 2: c }, { 3: e }];
How it is possible to achieve?
You could assign the objects with the wanted object. The result keeps the object references as well as the array reference.
const
array = [{ 1: 'a' }, { 2: 'b' }, { 3: 'd' }],
update = [{ 1: 'a' }, { 2: 'c' }, { 3: 'e' }];
array.forEach((o, i) => Object.assign(o, update[i]));
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you like only to update a certain property, you need to iterate the array and all entries.
const
array = [{ 1: 'a' }, { 2: 'b' }, { 3: 'd' }],
update = [{ 1: 'a' }, { 2: 'c' }, { 3: 'e' }];
array.forEach((o, i) => {
Object.entries(update[i]).forEach(([k, v]) => {
if (o[k] !== v) (console.log(o[k], v), o[k] = v);
})
});
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Get grouped array which has same value from array using lodash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have an array like this.
[{id: 1, name: "john", item:['a']},
{id: 1, name: "john", item:['b']},
{id: 2, name: "linda", item:['c']},
{id: 2, name: "linda", item:['d']},
{id: 3, name: "liam", item:['e']}
]
I hope to get grouped arrays having same id and name value like this
[{id: 1, name: "john", item:['a']},
{id: 1, name: "john", item:['b']}
],
[{id: 2, name: "linda", item:['c']},
{id: 2, name: "linda", item:['d']}
],
[{id: 3, name: "liam", item:['e']}
]
Please help me
One solution could be to store the items in a map (for simplicity an object) based on the id and then get the values from that object.
First, we have to check if we already have values stored for that id if that is the case we push the value to the array, if no we have to create the array.
const objMap = {};
items.forEach(item => {
const {id} = item;
if(objMap[id]) objMap[id].push(item);
else objMap[id] = [item];
})
const groupedValues = Object.values(objMap)
I hope that this solves your problem.

How to sync the indices of two arrays based on a shared value in Javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Given two arrays of the same length and identical content, how can one sort an array to be the in same order as the second array, based on a shared property?
Example:
let array1 = [{id: 123, ...}, {id: 456, ...}, {id: 789, ...}] // order always stays the same
let array2 = [{id: 456, ...}, {id: 789, ...}, {id: 123, ...}] // order is always different
How can I sort array1 such that:
array1[0].id is 456 and
array2[0].id is 456
Loop through the first and push to the second by index:
let arr1 = [{id: 123}, {id: 456}, {id: 789}] // order always stays the same
let arr2 = [{id: 456}, {id: 789}, {id: 123}]
arr1.forEach((obj1, idx) => {
arr2[idx] = obj1
})
console.log(arr1, arr2)
Assuming array2 has the objects we care about and array1 specifies the order, map over the ordering array using it to select the objects to be ordered...
let array1 = [{id: 123 }, {id: 456 }, {id: 789, }]
let array2 = [{id: 456, name: '456' }, {id: 789, name: '789' }, {id: 123, name: '123' }]
let array2ElementsSortedByArray1 = array1.map(e => {
return array2.find(e2 => e2.id === e.id)
})
console.log(array2ElementsSortedByArray1)
You can create a hash of the shape {id: index, ...} from array2 and then use this to order the elements of array1. This saves you the cost of find() on each iteration.
let array1 = [{ id: 123, }, { id: 456, }, { id: 789, }] // order always stays the same
let array2 = [{ id: 456, }, { id: 789, }, { id: 123, }]
const
indeces = Object.fromEntries(array2.map(({ id }, i) => [id, i])), // { 456: 0, 789: 1, 123: 2 }
ordered = [];
array1.forEach(o => ordered[indeces[o.id]] = { ...o });
console.log(ordered)

Unique objects from array based on two properties javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
lets say we have an array of objects:
results = [
{id: 1, name: John},
{id: 2, name: Gabo},
{id: 1, name: Anna},
{id: 3, name: Gabo}
{id: 1, name: Jack}, ]
I want function which gets all this objects which has unique id and name and it's value is not in other object.
results = [
{id: 1, name: John}, // unique id-name so we add to array
{id: 2, name: Gabo}, // unique id-name so we add to array
{id: 1, name: Anna}, // name is unique but object with this id already exists in array so we reject
{id: 3, name: Gabo} // id is unique but object with this name already exists in array so we reject
{id: 1, name: Jack}, ] //etc..
results = [
{id: 1, name: John},
{id: 2, name: Gabo},
You can use Set to register and then quickly check for duplicate id or name:
function getUniq(items) {
const ids = new Set();
const names = new Set();
return items.filter((item) => {
if (ids.has(item.id) || names.has(item.name)) {
return false;
}
ids.add(item.id);
names.add(item.name);
return true;
});
}

How to split array into three arrays by index? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have an array of 9 elements(maybe more).
I need to split by indexes into three arrays.
const array = [{id: 0}, {id: 1}, ..., {id: 8}];
I want to recieve 3 array like that:
const array0 = [{id: 0}, {id: 3}, {id: 6}];
const array1 = [{id: 1}, {id: 4}, {id: 7}];
const array2 = [{id: 2}, {id: 5}, {id: 8}];
You could take the remainfer of the index with three and address the dame group for every third element.
If necessary take destructuring assignment, like
[array0, array1, array2] = result;
const
array = [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }, { id: 6 }, { id: 7 }, { id: 8 }],
result = array.reduce((r, o, i) => ((r[i % 3] = r[i % 3] || []).push(o), r), []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can filter() the array using remainder operator %. For array1 check if the id % 3 === 0 and id % 3 === 1, id % 3 === 2 for next ones respectively
const array = Array(9).fill().map((x, i) => ({id: i}));
const array1 = array.filter(x => x.id % 3 === 0);
const array2 = array.filter(x => x.id % 3 === 1);
const array3 = array.filter(x => x.id % 3 === 2);
console.log('array1', array1)
console.log('array2', array2)
console.log('array3', array3)

Categories