I have the following code, where I have a set of obj b is where the way to group them are defined in a by elements.
I'm having some doubts about how to do it.
For example, is it correct to scroll a or b?
Can you give me a hand?
const a = [{
_id: 0,
elements: ['aasa', 'cccx', 'zzzx', 'sd']
},
{
_id: 1,
elements: ['bb', 'xx']
}
];
const b = [{
_id: 'aasa',
info: "sssas"
},
{
_id: 'bb'
},
{
_id: 'zzzx',
info: "ssss"
},
{
_id: 'cccx',
info: "sss"
},
{
_id: 'xx'
}
];
// result
[
[{
_id: 'aasa',
info: "sssas"
},
{
_id: 'zzzx',
info: "ssss"
},
{
_id: 'cccx',
info: "sss"
}
],
[{
_id: 'bb'
},
{
_id: 'xx'
}
]
];
c = a.map(el => el.elements)
const p = b
//.map(el => el.elements)
.reduce(function(prev, curr) {
//if
prev.push(curr);
return prev;
}, []);
//console.log("End",p)
You can first change b to be a lookup object (here I have used a Map), where each id is a key that points to the object itself:
// Transform `b` into a Map of the form:
Map {
'aasa' => {
_id: 'aasa',
info: "sssas"
},
'bb' => {
_id: 'bb'
}
// ... etc ...
}
Then, for each object in a, you can map over the elements array, and use the id from that object as a key in the lookup object to obtain the associated object with that id. Before you perform a map, you can first filter any keys (ie: ids) that don't exist in the lookup table by using .has on the Map:
const a = [{ _id: 0, elements: ['aasa', 'cccx', 'zzzx', 'sd'] }, { _id: 1, elements: ['bb', 'xx'] } ];
const b = [{ _id: 'aasa', info: "sssas" }, { _id: 'bb' }, { _id: 'zzzx', info: "ssss" }, { _id: 'cccx', info: "sss" }, { _id: 'xx' } ];
const lut = new Map(b.map(obj => [obj._id, obj]));
const res = a.map(({elements}) => elements.filter(key => lut.has(key)).map(key => lut.get(key)));
console.log(res);
This should work
const c = a.map(el1 => {
return b.filter(el2 => {
return el1.elements.includes(el2._id)
})
});
Related
I am trying to remove all the _er and _bx from the array, how can I do it? The way I tried doesn't seem to work. I'd like to see a solution where it removes all after _, and aswell only the letter that I put in for e.g remove all _ with er after.
const nullValue = {
collection: [{
name: "test_er"
},
{
name: "test_bx"
},
{
name: "fred"
},
{
name: "test_er"
}
]
};
const newArr = []
for (let [key, item] of nullValue.collection.entries()) {
item.name.replace(/_er/g, '')
newArr.push(item)
}
console.log(newArr)
Is this what you're looking for?
const nullValue = {
collection: [
{
name: 'test_er',
},
{
name: 'test_bx',
},
{
name: 'fred',
},
{
name: 'test_er',
},
],
};
nullValue.collection = [
...nullValue.collection.map(item => ({
name: item.name.replace(/_.*$/, ''),
})),
];
console.log(nullValue);
You can also use .split('_')[0] with the map method similar to Dmitry's answer... This gives you the first string of the split array, split at the underscore...
const nullValue = {
collection: [{
name: "test_er"
},
{
name: "test_bx"
},
{
name: "fred"
},
{
name: "test_er"
}
]
};
nullValue.collection = [ ...nullValue.collection.map( names => ({ name: names.name.split('_')[0], })),]
console.log(nullValue)
If you want to keep the original array of objects...
const nullValue = {
collection: [{
name: "test_er"
},
{
name: "test_bx"
},
{
name: "fred"
},
{
name: "test_er"
}
]
};
const newArr = { collection :
[ ...nullValue.collection.map( names =>
({ name: names.name.split('_')[0], })),
]}
console.log('newArr = ', newArr)
console.log('nullValue = ', nullValue)
You were VERY close with your original code, but the mistake was that String.replace() does not operate in-place, but rather returns its result. I've modified your code and added a comment below:
const nullValue = {
collection: [{
name: "test_er"
},
{
name: "test_bx"
},
{
name: "fred"
},
{
name: "test_er"
}
]
};
const newArr = []
for (let [key, item] of nullValue.collection.entries()) {
// My change is here
newArr.push( item.name.replace(/_er/g, '') )
}
console.log(newArr)
const nullValue = {
collection: [
{
name: "test_er"
},
{
name: "test_bx"
},
{
name: "fred"
},
{
name: "test_er"
}
]
};
nullValue.collection = nullValue.collection.map(i => i.name.replace(/_.*$/, ''))
console.log(nullValue)
This is preferable to .map() since you don't need a new array. You just want to change the strings:
const nullValue = {
collection: [
{ name: "test_er" },
{ name: "test_bx" },
{ name: "fred" },
{ name: "test_er" }
]
};
nullValue.collection.forEach(i => i.name = i.name.replace(/_.*$/, ''));
console.log(nullValue.collection);
i want to create a new array from api, but i don't know how to make it, i'm very confused in looping each array
This is each data
const group_one = [
{
name: "smash",
id: "012112"
},
{
name: "ahlan wa sahlan",
id: "123123"
},
{
name: "ahh",
id: "1231239"
},
{
name: "laki",
id: "21312"
}
];
const group_two = [
{
name: "ahh",
id: "1231239"
},
{
name: "laki",
id: "21312"
}
];
const group_three = [
{
name: "smash",
id: "012112"
},
{
name: "ahlan wa sahlan",
id: "123123"
}
];
this is the main data of api
const data = [
{
body: group_one,
group_id: "01"
},
{
body: grouop_two,
group_id: "02"
},
{
body: group_three,
group_id: "03"
}
];
export default data;
i want to create a new array like this, bcs i want to create a new object containing the group_id of each same data in the array
const newArray = [
{
name: "smash",
id: "012112",
group_id: ["01","03"]
},
{
name: "ahlan wa sahlan",
id: "123123",
group_id: ["01","03"]
},
{
name: "ahh",
id: "1231239",
group_id: ["01","02"]
},
{
name: "laki",
id: "21312",
group_id: ["01","02"]
}
];
can someone help me? with articles or codes.
thanks for helping me (sry for my bad english)
Please see below commented code:
const group01 = [
{
name: 'smash',
id: '012112'
},
{
name: 'ahlan wa sahlan',
id: '123123'
},
{
name: 'ahh',
id: '1231239'
},
{
name: 'laki',
id: '21312'
}
];
const group02 = [
{
name: 'ahh',
id: '1231239'
},
{
name: 'laki',
id: '21312'
}
];
const group03 = [
{
name: 'smash',
id: '012112'
},
{
name: 'ahlan wa sahlan',
id: '123123'
}
];
const data = [
{
body: group01,
group_id: '01'
},
{
body: group02,
group_id: '02'
},
{
body: group03,
group_id: '03'
}
];
function regroup(input) {
// USE Map FOR EASIER ITEM HANDLING.
const output = new Map();
// LOOP MAIN DATA ARRAY.
input.forEach(({body, group_id}) => {
// LOOP EACH GROUP.
body.forEach(({name, id}) => {
// USE id TO GET AN ITEM FROM output OR CREATE A NEW ONE IF IT DOES NOT EXIST.
const item = output.get(id) || {name, id, group_id: []};
// PUSH CURRENT group_id TO THE RESPECTIVE ARRAY.
item.group_id.push(group_id);
// SAVE ITEM TO OUTPUT Map AGAIN.
output.set(id, item);
});
});
// RETURN OUTPUT.
return Array.from(output.values());
}
const new_data = regroup(data);
console.log(new_data);
Having trouble wrapping my head around how to do this. I could have two arrays that I need to combine:
const users = [
{
user: 'user_1',
},
{
user: 'user_2',
},
{
user: 'user_3',
},
{
user: 'user_4',
},
]
and
const labels = [
{
label: 'label_1',
},
{
label: 'label_2',
},
]
After combining them the result should look as such:
const result = [
{ user: 'user_1', label: 'label_1' },
{ user: 'user_1', label: 'label_2' },
{ user: 'user_2', label: 'label_1' },
{ user: 'user_2', label: 'label_2' },
{ user: 'user_3', label: 'label_1' },
{ user: 'user_3', label: 'label_2' },
{ user: 'user_4', label: 'label_1' },
{ user: 'user_4', label: 'label_2' },
]
However, it could also be the case that users looks as such:
const users = [
{
user: 'user_1',
},
]
and labels as such:
const labels = [
{
label: 'label_1',
},
{
label: 'label_2',
},
{
label: 'label_3',
},
{
label: 'label_4',
},
]
whereas the result should then look as so:
const result = [
{ user: 'user_1', label: 'label_1' },
{ user: 'user_1', label: 'label_2' },
{ user: 'user_1', label: 'label_3' },
{ user: 'user_1', label: 'label_4' },
]
Please excuse if this is really trivial, have been staring at this for a while now.
It looks like, you need a cartesian product of the given arrays.
For this approach, you could take a function which returns the product of the arrays. For another array, you could easily add a new parameterin the call of the function.
const
cartesian = (...array) => array.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => ({ ...v, ...w }))), [])),
users = [{ user: 'user_1' }, {user: 'user_2' }, { user: 'user_3' }, { user: 'user_4' }],
labels = [{label: 'label_1' }, { label: 'label_2' }],
result = cartesian(users, labels);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I'd recommend to learn about functional features of javascript
users.map(e => labels.map(g => ({...e, ...g}) )).flat()
it iterates through both lists, creates a merged objects, and then since labels.map returns an array, we flatten it (e.g merge with original array)
Just use a nested loop:
const result = [];
for (const {user} of users) {
for (const {label} of labels) {
result.push({user, label});
}
}
console.log(result);
Or, without destructuring and merging all properties of the objects instead:
const result = [];
for (const user of users) {
for (const label of labels) {
result.push({...user, ...label});
}
}
console.log(result);
I'm wondering, I have the following data structure:
data = [
{
name: 'Alpha',
},
{
name: 'Alfa',
},
{
name: 'Bravo',
},
{
name: 'Brafo',
},
{
name: 'Charlie',
},
{
name: 'Charly',
},
...
{
name: 'Zulu',
},
{
name: 'Zulo',
},
]
I'm expecting there to be at least one, usually more, key for each letter of the alphabet. However, if there isn't a single data.name I would still like in the below data structure to have an empty domains array [].
I was wondering, how could this be manipulated into the following data structure:
data = {
a: {
domains: [
{
name: 'Alpha',
},
{
name: 'Alfa',
},
],
},
b: {
domains: [
...
]
},
...
z: {
domains: [
...
]
},
};
I have used a few methods, which involved a pre-constructed "alphbetised" key = object array, then filtered each on the first letter of the data.name value...but I was wondering if there was a standard and performant method to acheive this?
Using reduce()
const data = [{name:"Alpha"},{name:"Alfa"},{name:"Bravo"},{name:"Brafo"},{name:"Charlie"},{name:"Charly"},{name:"Zulu"},{name:"Zulo"}]
const res = data.reduce((a, v) => {
// prepare key
let key = v.name.substring(0,1).toLowerCase()
// check key in accumulator
if (!a[key]) {
// assign domain object
a[key] = {domains: []}
}
// push domain array
a[key].domains.push(v)
return a
}, {})
console.log(res)
Here is what you want:
data = [
{
name: 'Alpha',
},
{
name: 'Alfa',
},
{
name: 'Bravo',
},
{
name: 'Brafo',
},
{
name: 'Charlie',
},
{
name: 'Charly',
},
{
name: 'Zulu',
},
{
name: 'Zulo',
},
];
console.log(data.reduce((a, c) => {
const firstLetter = c.name[0].toLowerCase();
if (a[firstLetter]) {
a[firstLetter].domains.push(c);
} else {
a[firstLetter] = { domains: [c] };
}
return a;
}, {}));
Suppose there are two objects.
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
and the result
{
'1-1':[
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
],
'1-2':[
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
],
'2-1':[
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' },
]
}
Basically, I want to group the data.
I use includes to check if the item from b to match the id from a. Then construct the new array.
This is my attempt(fiddle):
return b.map(item => a.map(jtem => {
if(jtem.id.includes(item)){
return {
[item]: jtem
}
}
}))
For somehow, it doesn't work.
and, is there a clever way to avoid the nested for loop or map function?
You can do that in following steps:
Apply reduce() on the array b
During each iteration use filter() on the the array a
Get all the items from a which starts with item of b using String.prototype.startsWith()
At last set it as property of the ac and return ac
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = b.reduce((ac,b) => {
ac[b] = a.filter(x => x.id.startsWith(b));
return ac;
},{})
console.log(res)
As suggested by #Falco is the comments that It would be better to scan over the a once as its large. So here is that version.Actually its better regarding performance
const a = [
{ id: '1-1-1', name: 'a111' },
{ id: '1-1-2', name: 'a112' },
{ id: '1-2-1', name: 'a121' },
{ id: '1-2-2', name: 'a122' },
{ id: '2-1-1', name: 'a211' },
{ id: '2-1-2', name: 'a212' }
]
const b = ['1-1', '1-2', '2-1']
let res = a.reduce((ac,x) => {
let temp = b.find(y => x.id.startsWith(y))
if(!ac[temp]) ac[temp] = [];
ac[temp].push(x);
return ac;
},{})
console.log(res)
Note: startsWith is not supported by I.E. So you can create polyfill using indexOf
if(!String.prototype.startWith){
String.prototype.startsWith = function(str){
return this.indexOf(str) === 0
}
}