admin管理员组

文章数量:1316980

this is my first array

data=  [{
  "id":1111,
  "date":"2020-08-03T08:00:00+00:00",
  "age":23,
   "email":"[email protected]",
   "address":"phill road",
   "salary":1222.00
},

 {
  "id":222,
  "date":"2020-08-03T08:00:00+00:00",
  "age":24,
   "email":"[email protected]",
   "address":"phill2222 road",
   "salary":3344
}
]

second array should look like this

  dataNew=  [1222.00,3344]

I need second array that only contain the salary value . How can I filter the first array, that if the keys name = "salary" , push the value to new array. Searched for so many suggestion, but nothing seems working for me..

this is my first array

data=  [{
  "id":1111,
  "date":"2020-08-03T08:00:00+00:00",
  "age":23,
   "email":"[email protected]",
   "address":"phill road",
   "salary":1222.00
},

 {
  "id":222,
  "date":"2020-08-03T08:00:00+00:00",
  "age":24,
   "email":"[email protected]",
   "address":"phill2222 road",
   "salary":3344
}
]

second array should look like this

  dataNew=  [1222.00,3344]

I need second array that only contain the salary value . How can I filter the first array, that if the keys name = "salary" , push the value to new array. Searched for so many suggestion, but nothing seems working for me..

Share Improve this question asked Aug 5, 2020 at 15:39 user3643092user3643092 4361 gold badge8 silver badges21 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

data=  [{
  "id":1111,
  "date":"2020-08-03T08:00:00+00:00",
  "age":23,
   "email":"[email protected]",
   "address":"phill road",
   "salary":1222.00
},
 {
  "id":222,
  "date":"2020-08-03T08:00:00+00:00",
  "age":24,
   "email":"[email protected]",
   "address":"phill2222 road",
   "salary":3344
},
 {
  "id":223,
  "date":"2020-08-03T08:00:00+00:00",
  "age":24,
   "email":"[email protected]",
   "address":"phill2222 road"
}
]

console.log(data.filter(e => e.salary).map(e => e.salary));

This

data = [{
    "id": 1111,
    "date": "2020-08-03T08:00:00+00:00",
    "age": 23,
    "email": "[email protected]",
    "address": "phill road",
    "salary": 1222.00
  },

  {
    "id": 222,
    "date": "2020-08-03T08:00:00+00:00",
    "age": 24,
    "email": "[email protected]",
    "address": "phill2222 road",
    "salary": 3344
  }
]
arr = []
for (i = 0; i < data.length; i++) {
  console.log(data[i]['salary'])
  arr.push(data[i]['salary']);
}
console.log(arr)

data=  [{
  "id":1111,
  "date":"2020-08-03T08:00:00+00:00",
  "age":23,
   "email":"[email protected]",
   "address":"phill road",
   "salary":1222.00
},

 {
  "id":222,
  "date":"2020-08-03T08:00:00+00:00",
  "age":24,
   "email":"[email protected]",
   "address":"phill2222 road",
   "salary":3344
}]

data.map(item =>  item.salary)

You can use .map() on the array

data.map(item => item.salary)
data=  [{
  "id":1111,
  "date":"2020-08-03T08:00:00+00:00",
  "age":23,
   "email":"[email protected]",
   "address":"phill road",
   "salary":1222.00
},

 {
  "id":222,
  "date":"2020-08-03T08:00:00+00:00",
  "age":24,
   "email":"[email protected]",
   "address":"phill2222 road",
   "salary":3344
}
]
const dataNew=[]
data.map(item=>item.salary?dataNew.push(item.salary):null)

本文标签: How to filter an array by key and push the value to another array using javascriptStack Overflow