forked from rohan-paul/Awesome-JavaScript-Interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap-Implementation-On-Objects.js
61 lines (45 loc) · 1.34 KB
/
map-Implementation-On-Objects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// The map() method creates a new array with the results of calling a provided function on every element in this array.
// Problem - 1 We’ve an array of products, that has two properties: `id` and `name`. I want to get all ids, wih map() method:
let products = [
{
id: 0,
name: 'Product 1'
},
{
id: 1,
name: 'Product 2'
}
];
let productsIds = products.map((item) => {
return item.id;
})
// console.log(productsIds);
/* Problem-2
The map() mehod's syntax is as below
let newArr = oldArr.map((val, index, arr) => {
// return element to new Array
});
newArr — the new array that is returned
oldArr — the array to run the map function on
val — the current value being processed
index — the current index of the value being processed
arr — the original array
Create an object from a given array and the given array is as below
[1,2,3,4];
*/
let arr = [1,2,3,4];
let obj = arr.map((val, index, arr) => {
return {
VALUE : val,
INDEX: index
}
})
// console.log(obj);
// Problem-2 - Using map() return full name from the given array
var oldArr = [{first_name:"Colin",last_name:"Toh"},{first_name:"Addy",last_name:"Osmani"},{first_name:"Yehuda",last_name:"Katz"}];
let newArr = [];
oldArr.map((item, index) => {
item.full_name = [item.first_name, item.last_name].join(' ');
return item;
});
console.log(oldArr);