Stack Overflow Asked by Mr.Code on November 4, 2021
The first array is:
[
{
id: "megaphone",
name: "Megaphone",
kind: "Consumable",
description: "Unmutes you if you are currently muted",
price: 10,
},
{
id: "expcharge",
name: "Exp Charge",
kind: "Consumable",
description: "Double exp for an hour",
price: 50,
},
{
id: "commonlootbox",
name: "Common Lootbox",
kind: "Consumable",
description: "Chance for a random amount of coins or an item",
price: 1,
},
];
The second part of the JSON I need is into a JSON file.
I tried to add the data of the json like this:
arr.forEach((a) => {
no.push(shop.find((i) => i.id === a.name));
});
And this is perfect, because i get only the data i need.
But the result is that i have two arrays now.
The second is:
[
{ name: "megaphone", quantity: 5 },
{ name: "expcharge", quantity: "3" },
{ name: "commonlootbox", quantity: "3" },
];
Now, what I need to do is basically for Each object in the array, I need to do
"array1.name – array2.quantity (array1.description)"
I need to post it like
Megaphone – 5 (Unmutes you if you are currently muted)
Common Lootbox – 3 (Double exp for an hour)
etc.
Hope this is clear.
Maybe my method is wrong?
well, you need to itirate over the second array and make strings of the result that you want, like so
const secondArray = [
{name: 'megaphone', quantity: 5}
{name: 'expcharge', quantity: '3'}
{name: 'commonlootbox', quantity: '3'}
]
const desiredarray = secondArray.map(item => {
const matchedItem = data.find(({ id }) => id == item.name)
return `${matchedItem.name} - ${item.quantity} (${matchedItem.description})`
});
Answered by Ahmed Khattab on November 4, 2021
Ciao, you could do something like this:
const arr1 = [{id: 'megaphone', name: 'Megaphone', kind: 'Consumable', description: 'Unmutes you if you are currently muted', price: 10},
{id: 'expcharge', name: 'Exp Charge', kind: 'Consumable', description: 'Double exp for an hour', price: 50},
{id: 'commonlootbox', name: 'Common Lootbox', kind: 'Consumable', description: 'Chance for a random amount of coins or an item', price: 1}];
const arr2 = [{name: 'megaphone', quantity: 5},
{name: 'expcharge', quantity: '3'},
{name: 'commonlootbox', quantity: '3'}];
let arr3 = arr1.map(item1 => {
let ok_el = {};
arr2.map(item2 => {
if (item1.id === item2.name) {
ok_el.id = item1.id;
ok_el.name = item1.name;
ok_el.quantity = item2.quantity;
ok_el.description = item1.description;
return ok_el;
}
});
return ok_el;
});
let result = arr3.map(el => {
return el.name + " - " + el.quantity + " (" + el.description + ")";
});
console.log(result);
const arr1 = {0:{id: 'megaphone', name: 'Megaphone', kind: 'Consumable', description: 'Unmutes you if you are currently muted', price: 10},
1:{id: 'expcharge', name: 'Exp Charge', kind: 'Consumable', description: 'Double exp for an hour', price: 50},
2:{id: 'commonlootbox', name: 'Common Lootbox', kind: 'Consumable', description: 'Chance for a random amount of coins or an item', price: 1}};
const arr2 = {0:{name: 'megaphone', quantity: 5},
1:{name: 'expcharge', quantity: '3'},
2:{name: 'commonlootbox', quantity: '3'}};
let arr3 = Object.values(arr1).map(item1 => {
let ok_el = {};
Object.values(arr2).map(item2 => {
if (item1.id === item2.name) {
ok_el.id = item1.id;
ok_el.name = item1.name;
ok_el.quantity = item2.quantity;
ok_el.description = item1.description;
return ok_el;
}
});
return ok_el;
});
let result = arr3.map(el => {
return el.name + " - " + el.quantity + " (" + el.description + ")";
});
console.log(result);
Answered by Giovanni Esposito on November 4, 2021
I think you could store one or both arrays in a Map or directly in an object. You could try something like this:
obj = array.reduce((obj, element) => {
obj[element.id] = element;
return obj;
}, {});
Now you can iterate one of the arrays and access the corresponding value in the other one directly.
Answered by Mihail Feraru on November 4, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP