0

What is the use of making another array by using map ,so i need one reason to make a new array using map because i have watched many tutorials but didn't understand What is the difference between using this

     ids = data.allItems[type].map(function(current){
         return current.id;
     }) 
     index = ids.indexOf(id)
     console.log(index)
     if(index !== -1){
         data.allItems[type].splice(index,1) //[0,1,2,3,4]
     }

and this

    ids = data.allItems[type][id]
   index = data.allItems[type].indexOf(ids)
        if(index !== -1){
        data.allItems[type].splice(index,1) //[0,1,2,3,4]
    }

both are doing the same function

2
  • It's a little difficult to explain it with no knowledge of the context. If you can provide more information (the object you're using for example) I could try to explain to you why and if map is the best choice in such scenario. Commented Jun 21, 2020 at 0:36
  • I read a little about it and i think it's use is to make some categories of the Array such as to make an array of the age or name Commented Jun 21, 2020 at 1:00

2 Answers 2

1

map allows transforming the elements of an array, this implies several advantages

For example given the following list of employees

const employees = [
  {
    id: 1,
    firstName: "john",
    lastName: "doe",
    salary: 10000,
  },
  {
    id: 2,
    firstName: "andy",
    lastName: "johnson",
    salary: 15000,
  },
];

And suppose you require only their salaries, you can do the following

const salaries = employees.map((employee) => employee.salary);

// output [ 10000, 15000 ]

Now suppose you need to get their salaries next to their full names, you can try the following

const employeeList = employees.map(({ id, firstName, lastName, salary }) => ({
  id,
  fullName: `${firstName} ${lastName}`,
  salary,
}));

// output [{ fullName: 'john doe', salary: 10000 }, { fullName: 'andy johnson', salary: 15000 }]

You accomplish this without loops, which is a plus

Another advantage is that it allows you to avoid unexpected results, for example imagine that you need a copy of the list of employees

const newEmployees = employees;

And then you modify the salary of one of the employees from the original list

employees[0].salary = 25000;

This operation will affect both lists, as you can see here

console.log(JSON.stringify(employees, null, 2));
console.log(JSON.stringify(newEmployees, null, 2));

output

[
  {
    "id": 1,
    "firstName": "john",
    "lastName": "doe",
    "salary": 25000
  },
  {
    "id": 2,
    "firstName": "andy",
    "lastName": "johnson",
    "salary": 15000
  }
]
[
  {
    "id": 1,
    "firstName": "john",
    "lastName": "doe",
    "salary": 25000
  },
  {
    "id": 2,
    "firstName": "andy",
    "lastName": "johnson",
    "salary": 15000
  }
]

John has salary of 25000 in the original list and the new list. The reason is that copies by reference

To avoid this, map allows you to copy to get a new list and in consequence free of unexpected results

const anotherNewEmployess = employees.map(
  ({ id, firstName, lastName, salary }) => ({ id, firstName, lastName, salary })
);

output

[
  {
    "id": 1,
    "firstName": "john",
    "lastName": "doe",
    "salary": 25000
  },
  {
    "id": 2,
    "firstName": "andy",
    "lastName": "johnson",
    "salary": 15000
  }
]
[
  {
    "id": 1,
    "firstName": "john",
    "lastName": "doe",
    "salary": 10000
  },
  {
    "id": 2,
    "firstName": "andy",
    "lastName": "johnson",
    "salary": 15000
  }
]

In this example there are better ways to do it, or more compact ones like Array.from() or destructuringor some other hidden jutsu that SO boys know

The latter is very relevant in the context of functional programming, where the data is immutable. And many libraries and frameworks adopt it and recommend it

I have presented an example other than yours because I believe it is more easily demonstrates the usefulness of map

I hope this explanation is useful to you

Sign up to request clarification or add additional context in comments.

Comments

1

It is just what other projection/map/select functions do. It's in the JS spec. In your 2nd example, if ids is an array of objects, then it won't work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.