create arrays from array containing similar path items [duplicate] - javascript

This question already has answers here:
How can I group an array of objects by key?
(32 answers)
Closed 7 months ago.
I have array like this
Var arr =[
["ali", "classroom1"],
["john", "classroom1"],
["Doe", "classroom1"],
["ahmed", "classroom2"],
["Mo", "classroom2"],
["Wael", "classroom3"]
]
Now I want to create arrays or slit it to get array for classroom1 and different array for classroom2 .....
I used splice and map but it seems they're not used for this

Consider something like this:
let arr =[
["ali", "classroom1"],
["john", "classroom1"],
["Doe", "classroom1"],
["ahmed", "classroom2"],
["Mo", "classroom2"],
["Wael", "classroom3"]
];
let classrooms = new Map();
for (let item of arr) {
const [name, classroom] = item;
if (!classrooms.has(classroom)) {
classrooms.set(classroom, []);
}
classrooms.get(classroom).push(name);
}
console.log(classrooms);

Related

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));

find array value that index matches another array values [duplicate]

This question already has answers here:
How to select elements from an array based on the indices of another array in JavaScript?
(3 answers)
Closed 2 years ago.
I have the following 2 arrays
tokenIds = [0,1,3]
and
strings = ["hello", "foo", "goodbye", "bar"]
what would be the best way to create a new array with the elements in "strings" array which index's match the values of "tokenIds" array?
Such as creating a new array like the following
newstrings = ["hello", "foo", "bar"]
I have tried the following:
const newstrings = strings.filter(index => tokenIds.includes(tokenIds.index));
Thanks in advance!
Just map the tokenIds array:
const newstrings = tokenIds.map(i => strings[i]);
const tokenIds = [0,1,3]
const strings = ["hello", "foo", "goodbye", "bar"]
const result = strings.filter((str,idx) => tokenIds.includes(idx))
console.log(result)
May not be the best way to do it, but produces the result you require.
tokenIds = [0,1,3]
strings = ["hello", "foo", "goodbye", "bar"]
newstrings = []
for(var id in tokenIds)
{
newstrings.push(strings[tokenIds[id]]);
}
console.log(newstrings);

Column from array of arrays if another column match criteria in JavaScript [duplicate]

This question already has an answer here:
How to filter an array of arrays?
(1 answer)
Closed 3 years ago.
I have following array of arrays showing employee name, id and department
var employees = [
["John","001","Sales"]
["Mike","002","Sales"]
["Adam","003","Eng"]
["Sam","004","Sales"]
["Emma","005","Eng"]
];
how can I get specific column from this array based on another column. For example, I would like to get ID numbers column for all 'Eng' department where result would be:
["003","005"]
At first you need to filter your array and then just get necessary values through map method:
var employees = [
["John","001","Sales"],
["Mike","002","Sales"],
["Adam","003","Eng"],
["Sam","004","Sales"],
["Emma","005","Eng"]
];
const result = employees.filter(f=> f.some(s=>s == 'Eng')).map(([a, b, c]) => b);
console.log(result);
You can use map() on the array to filter through and get the data you want.
var employees = [
["John", "001", "Sales"],
["Mike", "002", "Sales"],
["Adam", "003", "Eng"],
["Sam", "004", "Sales"],
["Emma", "005", "Eng"]
];
function getColumnFor(data, dept) {
const output = [];
data.map(function (item) {
if (item[2].toLowerCase() === dept.toLowerCase()) {
output.push(item[1]);
}
});
return output;
}
const result = getColumnFor(employees, "Eng");
console.log(result);
If your array in formate, you can do as below
var employees = [
["John","001","Sales"],
["Mike","002","Sales"],
["Adam","003","Eng"],
["Sam","004","Sales"],
["Emma","005","Eng"]
];
var codes = [];
for(var k in employees){
if(employees[k][2] == 'Eng'){
codes.push(employees[k][1]);
}
}
console.log(codes);

How can I create keys for an array in Javascript? [duplicate]

This question already has answers here:
JS : Convert Array of Strings to Array of Objects
(1 answer)
Convert array of strings to objects and assign specific key
(1 answer)
Closed 3 years ago.
I would like to know how can I create keys for an array of values in javascript like this?
const array = ["NodeJs", "Javascript", "React"]
and transform it like this
const array = [{"name": "NodeJs"}, {"name": "Javascript"}, {"name": "React"}]
Using Array.prototype.map:
const arr = ["NodeJs", "Javascript", "React"];
const newArr = arr.map(name => ({name})); // or arr.map(item => ({name: item}))
console.log(newArr);
You can also do it using Array.from(iterable, mappingFunction) which will return a new array with objects {name: name}:
const array = ["NodeJs", "Javascript", "React"];
const mapArray = Array.from(array, (name) => ({name}));
console.log(mapArray);

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