JavaScript Array Method Exercises 02

preview_player
Показать описание
In this video, you'll get some more practice with JavaScript Array Methods by completing some practical exercises.

01:10 Exercise One (02:05 Final Solution)
02:15 Exercise Two (03:23 Final Solution)
03:45 Exercise Three (04:43 Final Solution)
04:50 Exercise Four (05:26 Final Solution)
05:54 Exercise Five (07:30 Final Solution)

In the tutorial, you'll learn about a few more different array method available in JavaScript by completing some simple exercises on an array of data which is loosely based around some ecommerce orders.

Don't worry if you get stuck - i've provided example solutions but feel free to post your own if you come up with something different.

#JavaScript #Practice #Exercises Channel Handle @codebubb
Рекомендации по теме
Комментарии
Автор

Got your own solution to one of the exercises? Post it as a comment below!


codewithbubb
Автор

const productIDSold = orders => orders.some(order => order.delivered && order.items.some(item => productId = '123'));
I really appreciate the effort you put into these useful exercises. Thank you. Hope my code is ok

robostudio
Автор

5) const sold = orders.some(order => order.items.some(item => item.productId === '123')) is it right?

motivation_
Автор

Great vid, 1 question - why does the object you create in the second example have to be inside parentheses?

badselection
Автор

#5

//have any products with an id of 123 been sold?
const isSold = orders.reduce((acc, order) => [...acc, ...order.items], []).some(item => item.productId === '123')

//how much the product have been sold?
const howMuchSold = orders.reduce((acc, order) => [...acc, ...order.items], []).filter(item => item.productId === '123').length

Finally, I began to understand a little better how the method Reduce works :)

toscaantosca
Автор

5) const anyProductsSold = orders.some(order => order.items.find(ord => ord.productId === '123')) Is it right?

maximemenelikmbengongone
Автор

//5 Have any products with an id of 123 been sold? return orders.filter(item=> item.orderId === '123')[0].items.length > 0

marcoonlinetv
Автор

another possible solution for Exercise # 5
orders.flatMap(order => order.items).some(item => item.productId === '123')

Game-viif
Автор

Ex-5 using For method

let count = 0;
for (x of orders) {
x.items.forEach(element => {
if (element.productId === "123") count++
})
}
console.log(count);

priyanshukatuwal
Автор

excercise 5:
const orders = (val) => val.filter( order => order.items.filter(item => item.productId === '123').length);

elliott_
Автор

my solution for exercise 5:

orders.filter(order => order.items.some(item => item.productId === '123')).length

Sam-czck
Автор

orders.some((order, items) => (order.items.filter(item => item.productId === "123").length > 0)); //it worked, i hope it is correct.

amritkjha
Автор

Hello, can somebody explain to me why this doesnt work for ex.5? Ty

toni
Автор

I had ex.3 solution:

orders.map(order => order.delivered === true ? 'yes' : 'no').includes('no') ? 'no' : 'yes';

and ex. 5:

orders.map(order => order.items.map(item => item.productId).join(' ')).filter(id => id.includes('123')).length;

this are too long solutions :)))

magistrfox
Автор

Problem Number 5: const orderCheck = () =>
orders.filter(
(order) => order.items.filter((item) => item.productId === "123").length,
).length;

rezaulmasum
Автор

ex5:

orders.some((item) => item.delivered && item.items.some((elem) => elem.productId === "123"));

saptarsidebnath
Автор

Great :)
orders.reduce(order => order.customerId === '123'); // false why is showing false ? EX:4

gheorgheslicari
Автор

For Exercise 5:
Have any products with an id of 123 been sold?
orders.some((order) => order.items.find((item) => item.productId === "123")); //true

aakashpoly