I have an object data as follows:
[
{
name: "Green Tea Brownie",
price: 60,
amount: 10,
seller: {
seller_id: 124,
seller_name: "Merry Shop"
}
},
{
name: "Cocoa Chiffon",
price: 20,
amount: 50,
seller: {
seller_id: 124,
seller_name: "Merry Shop"
}
},
{
name: "Milky Donut",
price: 40,
amount: 100
seller: {
seller_id: 421,
seller_name: "Sweet Bakery"
}
}
]
So I want to group data by "seller_id" and merge top level data assigns to object name "orders", just look like as following:
[
{
seller_id: 124,
seller_name: "Merry Shop",
orders: [
{
name: "Green Tea Brownie",
price: 60,
amount: 10
},
{
name: "Cocoa Chiffon",
price: 20,
amount: 50
}
]
},
{
seller_id: 421,
seller_name: "Sweet Bakery",
orders: [
{
name: "Milky Donut",
price: 40,
amount: 100
}
]
}
]
I tried to solve this problem several hours ago. Can anyone solve this case?
Thank you
You can use _.groupBy() and then _.map() the groups to requested format:
const { flow, partialRight: pr, groupBy, map, first, omit } = _
const fn = flow(
pr(groupBy, 'seller.seller_id'),
pr(map, group => ({
...first(group).seller,
orders: map(group, pr(omit, 'seller'))
}))
)
const data = [{"name":"Green Tea Brownie","price":60,"amount":10,"seller":{"seller_id":124,"seller_name":"Merry Shop"}},{"name":"Cocoa Chiffon","price":20,"amount":50,"seller":{"seller_id":124,"seller_name":"Merry Shop"}},{"name":"Milky Donut","price":40,"amount":100,"seller":{"seller_id":421,"seller_name":"Sweet Bakery"}}]
const result = fn(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
If you like to do with plain JS you can use reduce
const data = [{"name":"Green Tea Brownie","price":60,"amount":10,"seller":{"seller_id":124,"seller_name":"Merry Shop"}},{"name":"Cocoa Chiffon","price":20,"amount":50,"seller":{"seller_id":124,"seller_name":"Merry Shop"}},{"name":"Milky Donut","price":40,"amount":100,"seller":{"seller_id":421,"seller_name":"Sweet Bakery"}}]
const output = data.reduce((op,{name,price,amount,seller})=>{
if(op[seller['seller_id']]){
op[seller['seller_id']]['orders'].push({name,price,amount})
}
else {
op[seller['seller_id']] = {
seller_name: seller.seller_name,
seller_id: seller.seller_id,
orders: [{name,price,amount}]
}
}
return op;
},{})
console.log(Object.values(output))
Here is a pure JS solution using reduce:
const orders = [{
name: "Green Tea Brownie",
price: 60,
amount: 10,
seller: {
seller_id: 124,
seller_name: "Merry Shop"
}
}, {
name: "Cocoa Chiffon",
price: 20,
amount: 50,
seller: {
seller_id: 124,
seller_name: "Merry Shop"
}
}, {
name: "Milky Donut",
price: 40,
amount: 100,
seller: {
seller_id: 421,
seller_name: "Sweet Bakery"
}
}];
const ordersBySeller = Object.values(orders.reduce((accum, { name, price, amount, seller: { seller_id, seller_name } }) => {
const sellerOrders = accum[seller_id] ? accum[seller_id].orders || [] : [];
accum[seller_id] = { seller_id, seller_name, orders: [...sellerOrders, { name, price, amount } ] };
return accum;
}, {}));
console.log(ordersBySeller);
Related
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 1 year ago.
Improve this question
I am making a food delivery app and I have two different array of objects. One of them is cartItems and the other is foodItems. The arrays can be of different sizes. So what do I want to loop over each array and check
if the ids of both the array match.
Note I want to check if the quantity exists then increment it by the new quantity else simply add a new quantity
check if itemDetails exists in foodItems array and if it exists, check if the price of cartItems matches of that foodItem, then update the cartItems object, else remove them.
if itemDetails does not exist then update the quantity of the item.
Update
If there are two items with similar id and price, the quantities should be added
Here is my cartItems:
let cartItems = [
{ id: 1, price: 120, quantity: 7 },
{ id: 2, price: 70, quantity: 4 },
{ id: 1, price: 70, quantity: 3 },
{ id: 3, price: 60, quantity: 1 },
{id: 1, price: 120, quantity: 2}
];
Here is my foodItems
let foodItems = [
{
id: 1,
name: "chicken",
itemDetails: [
{
price: 120,
details: "2 pcs of chicken biryani"
},
{
price: 70,
details: "1 pcs of chicken biryani"
}
],
},
{
id: 2,
name: "Mutton",
itemDetails: [
{
price: 120,
details: "Two pieces of mutton biryani",
},
{
price: 70,
details: "one pcs of mutton biryani"
},
],
},
{ id: 3, name: "Ice Cream", price: 60 },
];
This is my desired output
let filteredArrayOuput = [
{
id: 1,
name: "Chicken Biryani",
itemDetails: [
{
price: 120,
details: "Two pieces of chicken Biryani",
},
],
quantity: 7,
},
{
id: 2,
name: "Mutton Biryani",
itemDetails: [
{
price: 70,
details: "Two pieces of mutton biryani",
},
],
quantity: 4,
},
{
id: 1,
price: "Chicken Biryani",
quantity: 3,
itemDetails: [
{
price: 70,
details: "Two pieces of Chicken Biryani",
},
],
},
{ id: 3, price: 60, quantity: 1 },
];
This is what I have done till now
const filterFunc = (arr, price) => {
let filtered = arr.filter((item) => {
return item.price == price;
});
return filtered;
};
const filterArray = (arr1, arr2) => {
const filtered = arr2.filter((el) => {
let arr = arr1.find(({ id, quantity, price }) => {
if (el.id === id) {
if (el.itemDetails !== undefined && el.itemDetails.length !== 0) {
let itemDetails = el.itemDetails;
return (
(el.quantity = quantity),
(el.itemDetails = filterFunc(itemDetails, price))
);
} else {
return (el.quantity = quantity);
}
}
});
return arr;
});
return filtered;
};
console.log(filterArray(cartItems, foodItems))
You can check the below code.
Find existingFoodItem from FoodItems array
Find priceObj by comparing price
return new object with price details if itemDetails exists (checking with ?), else without price if no itemDetails exists.
let cartItems = [
{ id: 1, price: 120, quantity: 7 },
{ id: 1, price: 120, quantity: 1 },
{ id: 2, price: 70, quantity: 4 },
{ id: 1, price: 70, quantity: 3 },
{ id: 3, price: 60, quantity: 1 },
];
let foodItems = [
{
id: 1,
name: "chicken",
itemDetails: [
{
price: 120,
details: "2 pcs of chicken biryani"
},
{
price: 70,
details: "1 pcs of chicken biryani"
}
],
},
{
id: 2,
name: "Mutton",
itemDetails: [
{
price: 120,
details: "Two pieces of mutton biryani",
},
{
price: 70,
details: "one pcs of mutton biryani"
},
],
},
{ id: 3, name: "Ice Cream", price: 60 },
];
let result = [];
cartItems.forEach(cart => {
let esitingItem = result.find(r => r.id === cart.id && r.itemDetails.find(i => i.price === cart.price));
if(esitingItem){
esitingItem.quantity += cart.quantity;
return;
}
let existingFoodItem = foodItems.find(food => food.id === cart.id);
if(existingFoodItem){
let priceObj = existingFoodItem.itemDetails?.find(item => item.price === cart.price);
if(priceObj){
result.push({id:cart.id,name:existingFoodItem.name,itemDetails:[{...priceObj}],quantity:cart.quantity});
}
else{
return result.push({id:cart.id,name:existingFoodItem.name,quantity:cart.quantity});
}
}
});
console.log(result);
const data =
[{notification_id: 124, user_id: 10, story_id: 25, string: "liked on your story" }
{notification_id: 125, user_id: 12, story_id: 25, string: "liked on your story" }
{notification_id: 126, user_id: 15, story_id: 25, string: "liked on your story" }]
output: user 10,12 and 15 liked on your story ID 25
I want to show output like above. How to merge and show those like the output. Any idea?
You need to create a hash map, check the code snippet below:
const allData = [
{ name: 'John', story: 1 },
{ name: 'Ross', story: 2 },
{ name: 'Taylor', story: 1 },
{ name: 'Jimmy', story: 2 },
{ name: 'Amanda', story: 3 },
];
const hash = {};
for (let data of allData) {
if (data.story in hash) hash[data.story] = [...hash[data.story], { ...data }];
else hash[data.story] = [{ ...data }];
}
console.log(hash);
You should use Map, which is what I would suggest. The code below does the same thing but using Maps.
const allData = [
{ name: 'John', story: 1 },
{ name: 'Ross', story: 2 },
{ name: 'Taylor', story: 1 },
{ name: 'Jimmy', story: 2 },
{ name: 'Amanda', story: 3 },
];
const hash = new Map();
for(let data of allData) {
const currentVal = hash.get(data.story);
if (currentVal) hash.set(data.story, [...currentVal, {...data}])
else hash.set(data.story, [{...data}])
}
console.log(hash);
I cann't comment. So i write here!
Are you wanting ...
result = [
{
story_id : 25,
user_id : [10, 12, 15]
}
]
Right?
This is solution of me
const data =
[{notification_id: 124, user_id: 10, story_id: 25, string: "liked on your story" }
{notification_id: 125, user_id: 12, story_id: 25, string: "liked on your story" }
{notification_id: 126, user_id: 15, story_id: 25, string: "liked on your story" }]
var result = data.reduce((res, item) => {
let storyID = item.story_id;
if (typeof res[storyID] == 'undefined') {
res[storyID] = {
story_id: storyID,
user_id: []
}
}
res[storyID].user_id.push(item.user_id);
return res;
}, []);
result = Object.values(result);
I try to filter values from the next:
const data = [
{
car: 'audi',
age: 2,
power: 200
},
{
car: 'bmw',
age: 32,
power: 100
},
{
car: 'mercedes',
age: 21,
power: 150
},
];
const getRes = (data, keyTxt) => {
return data.map(i => {
return {
[keyTxt]: (
Object.entries(i).filter(j => !'car'.includes(j))
)
}
})
};
console.log(getRes(data, 'age'));
I try to check if word car exists in Object.entries(), and if it exists to delete all arrays which include the word and to get something like this:
{
"age": [
[
"age",
2
],
[
"power",
200
]
]
},
{
"age": [
[
"age",
32
],
[
"power",
100
]
]
},
{
"age": [
[
"age",
21
],
[
"power",
150
]
]
}
What is wrong with my code and why the filter does no work? How to make this code to be able to get what i described above?
Just a slight correction in .includes
const data = [
{
car: 'audi',
age: 2,
power: 200
},
{
car: 'bmw',
age: 32,
power: 100
},
{
car: 'mercedes',
age: 21,
power: 150
},
];
const getRes = (data, keyTxt) => {
return data.map(i => {
return {
[keyTxt]: (
Object.entries(i).filter(j => !j.includes('car'))
)
}
})
};
console.log(getRes(data, 'age'));
Should be something like this:
Object.entries(i).filter(j => !j.includes('car'))
You have to either destructure the entries or reference them by index.
const getRes = (data, keyTxt) => {
return data.map((entry) => {
return {
[keyTxt]: Object.entries(entry).filter(([key]) => key != 'car')
};
});
};
You could use .map() and use delete to delete the property you dont need. Then return an Object with with your dynamic key name and Object.entries() as its value
const data = [
{
car: 'audi',
age: 2,
power: 200
},
{
car: 'bmw',
age: 32,
power: 100
},
{
car: 'mercedes',
age: 21,
power: 150
},
];
let result = (data, txt) => {
return data.map(el => {
delete el.car;
return {
[txt]: Object.entries(el)
};
})
}
console.log(result(data, "age"));
const prod = [{
name: "Sweat",
description: " collection",
price: 150,
},
{
name: "Trousers",
description: "Attire",
price: 243
},
{
name: "T-shirt",
description: "Winter",
},
{
name: "Hoody",
description: "Fashion",
},
{
name: "Pants",
description: "Winter",
},
{
name: "Casual",
description: "Winter",
price: 245,
},
{
name: "Shirt",
description: "Attire",
price: 150,
}
];
Hi, I'm trying to add a random popularity score between 0 - 100, randomly for the products without them using a function.
I've tried to figure out solutions from
https://levelup.gitconnected.com/set-data-structure-in-javascript-62e65908a0e6
and
https://medium.com/front-end-weekly/getting-a-random-item-from-an-array-43e7e18e8796
but still unsure how to add elements to specific indices without the 'popularity' element. Thanks!
Filter the array to the elements you want first, then apply the random number
// function
const addRandomPopularityWhereThereIsNone = products => {
products.filter(p => !p.hasOwnProperty('popularity')).forEach(p => {
p.popularity = Math.floor(Math.random() * 101)
})
}
// call it
addRandomPopularityWhereThereIsNone(products)
Note that this modifies the original array.
For reference:
Array.prototype.filter()
Object.protytype.hasOwnProperty()
Please try the following solution
const products = [{"name":"Pullover Sweat","description":"Winter collection","price":150,"popularity":99},{"name":"Formal Trousers","description":"Attire for men","price":500},{"name":"Winter T-shirt","description":"Winter collection","price":50,"popularity":50},{"name":"New Fashion Hoody","description":"Fashion line","price":200},{"name":"Winter Pants","description":"Winter collection","price":150},{"name":"Casual Coat","description":"Winter collection","price":245,"popularity":78},{"name":"Fine Long Sleeve Shirt","description":"Attire for men","price":150,"popularity":10}];
const output = products.map((product) => {
if ("popularity" in product) {
return { ...product };
}
return { ...product, popularity: generateRandomNumber() };
});
function generateRandomNumber() {
return Math.floor(Math.random() * 100) + 1;
}
console.log(output);
Take a look to array map and the in operator
Use map and nullish coalescing operator (??)
const products = [{"name":"Pullover Sweat","description":"Winter collection","price":150,"popularity":99},{"name":"Formal Trousers","description":"Attire for men","price":500},{"name":"Winter T-shirt","description":"Winter collection","price":50,"popularity":50},{"name":"New Fashion Hoody","description":"Fashion line","price":200},{"name":"Winter Pants","description":"Winter collection","price":150},{"name":"Casual Coat","description":"Winter collection","price":245,"popularity":78},{"name":"Fine Long Sleeve Shirt","description":"Attire for men","price":150,"popularity":10}];
const update = (arr) =>
arr.map(({ popularity, ...product }) => ({
popularity: popularity ?? Math.floor(Math.random() * 100) + 1,
...product,
}));
console.log(update(products));
const products = [{
name: "Pullover Sweat",
description: "Winter collection",
price: 150,
popularity: 99
},
{
name: "Formal Trousers",
description: "Attire for men",
price: 500
},
{
name: "Winter T-shirt",
description: "Winter collection",
price: 50,
popularity: 50
},
{
name: "New Fashion Hoody",
description: "Fashion line",
price: 200
},
{
name: "Winter Pants",
description: "Winter collection",
price: 150
},
{
name: "Casual Coat",
description: "Winter collection",
price: 245,
popularity: 78
},
{
name: "Fine Long Sleeve Shirt",
description: "Attire for men",
price: 150,
popularity: 10
}
];
const addPopularity = products => {
products.filter(p => !p.popularity).map(p => {
p.popularity = Math.floor(Math.random() * 101)
})
return products;
}
console.log(addPopularity(products));
I am trying to learn how destructuring works and encountered a challenge. I destructured results into a data variable and I was wondering how I would further destructure itemsInCart and buyerCountry.
function makeArray() {
return {
results: [
{
itemsInCart: [
{
name: "pizza",
price: 74,
qty: 1
},
{
name: "Pepper Soup",
price: 32,
qty: 2
}
],
buyerCountry: "Rwanda"
}
]
};
}
const {
results: [data]
} = makeArray();
console.log(data);
below is my output so far:
{
itemsInCart: [{
name: 'pizza',
price: 74,
qty: 1
},
{
name: 'Pepper Soup',
price: 32,
qty: 2
}
],
buyerCountry: 'Rwanda'
} => undefined
One approach would be to further destructure the data object that you've obtained by doing the following:
/* Your current destructuring */
const { results: [data] } = makeArray();
/* Additional destructuring step to get itemsInCard and buyerCountry */
const { itemsInCart, buyerCountry } = data;
console.log(itemsInCart, buyerCountry);
This can also be reduced into a single line by the following:
function makeArray() {
return {
results: [{
itemsInCart: [{
name: "pizza",
price: 74,
qty: 1
},
{
name: "Pepper Soup",
price: 32,
qty: 2
}
],
buyerCountry: "Rwanda"
}]
}
};
const { results: [{ itemsInCart, buyerCountry }] } = makeArray();
console.log('itemsInCart:', itemsInCart);
console.log('buyerCountry:', buyerCountry);