How to get a particular attribute from an array of array objects?

221b

I have an Array of Arrays, and each Array consists of objects. Here is a simplified version of what I'm referring to (it is a console.log of my original array) -

Array - [Array(2), Array(3), Array(2)]

Each Array has objects in the following format (taking the first array from above) -

Array(2) - 
0: {name: "test", score:40, date: "2018-09-18T00:00:00.000Z"}
1: {name: "test2", score:50 date: "2018-09-18T00:00:00.000Z"}

The other arrays are similar with the same attributes and different values.

I am trying to fetch the name attribute from each of these objects. I tried the below code - but I end up getting an undefined value:

    const test1= array1.map(x=> x.values) // this gives me the array of arrays
    const test2 = test1.map(function(y){return y.name})// this is my attempt to get the 'name' attribute from all of the arrays that include the objects.

What am I missing out on here? Is there a better way to get the attribute using arrow functions ?

Mohammed Ashfaq

/* TEST DATA */
array1 = [
  { name: 'test1', score: 40, date: '2018-09-18T00:00:00.000Z' },
];
array2 = [
  { name: 'test4', score: 50, date: '2018-09-18T00:00:00.000Z' },
  { name: 'test5', score: 40, date: '2018-09-18T00:00:00.000Z' }, 
];
array3 = [
  { name: 'test6', score: 50, date: '2018-09-18T00:00:00.000Z' },
  { name: 'test7', score: 50, date: '2018-09-18T00:00:00.000Z' },
  { name: 'test8', score: 40, date: '2018-09-18T00:00:00.000Z' },
  { name: 'test9', score: 50, date: '2018-09-18T00:00:00.000Z' },
];

testResults = [array1, array2, array3];

// Solution 

function getListOfName(){
  let names = [];
  testResults.map(testResult => {
    testResult.map(({name}) => {if(name) names.push(name)})
  })
  return names;
}
console.log("Full list of names", getListOfName());

// If you want to restrict to K names from each array
function getFirstKNamesfromArray(limit){
  let names = [];
  testResults.map(testResult => {
    testResult.map(({name}, index) => {
      if(name && (index < limit)) names.push(name)
    })
  })
  return names
}
console.log("First 2 names from each array", getFirstKNamesfromArray(2));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to get unique an array of objects back from a complex an array of objects?

分類Dev

how to get a particular object value from nested json array

分類Dev

How to get an Array of Objects from Firestore in Swift?

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

How to delete particular no of elements from array in mongodb

分類Dev

How to get the sum of number from an array of objects, using pure JS?

分類Dev

Get the object with the lowest value attribute from array

分類Dev

How to get particular hashmap keys and values from json objects

分類Dev

Get object from array with NSDictionary objects

分類Dev

Counting the particular words from array in other array

分類Dev

How to get all values of objects inside array

分類Dev

How to search for a value in array of objects and get it in Laravel?

分類Dev

Get average value from array consisting of objects based on objects fields

分類Dev

How to get an array of counts of specific objects in an existing array?

分類Dev

How to remove duplicates objects from array in javascript?

分類Dev

How to extract a property from array of objects and slice it?

分類Dev

How to put values of objects from an array into a string

分類Dev

How to change json data format from array to array of objects with javascript?

分類Dev

Get key value from a HTML attribute array javascript

分類Dev

using LINQ to get a particular set of objects from a list of objects

分類Dev

Obtaining lines from an array and stopping at a particular character

分類Dev

How to find the random value for a particular string in an array

分類Dev

How can I convert an array of arrays to an array of objects, selecting the key from other array?

分類Dev

How to get a the sum of multiple arrays within an array of objects?

分類Dev

How to get only one value in Javascript array of objects using for of and for in statements?

分類Dev

How to get matching elements from array and change array object?

分類Dev

How to get the keys as string array from map?

分類Dev

How to get numeric value from cURL array?

分類Dev

MongoDB: how to get specific elements from array?

Related 関連記事

  1. 1

    How to get unique an array of objects back from a complex an array of objects?

  2. 2

    how to get a particular object value from nested json array

  3. 3

    How to get an Array of Objects from Firestore in Swift?

  4. 4

    How to get the combination of array values from nested arrays in an array of objects

  5. 5

    How to delete particular no of elements from array in mongodb

  6. 6

    How to get the sum of number from an array of objects, using pure JS?

  7. 7

    Get the object with the lowest value attribute from array

  8. 8

    How to get particular hashmap keys and values from json objects

  9. 9

    Get object from array with NSDictionary objects

  10. 10

    Counting the particular words from array in other array

  11. 11

    How to get all values of objects inside array

  12. 12

    How to search for a value in array of objects and get it in Laravel?

  13. 13

    Get average value from array consisting of objects based on objects fields

  14. 14

    How to get an array of counts of specific objects in an existing array?

  15. 15

    How to remove duplicates objects from array in javascript?

  16. 16

    How to extract a property from array of objects and slice it?

  17. 17

    How to put values of objects from an array into a string

  18. 18

    How to change json data format from array to array of objects with javascript?

  19. 19

    Get key value from a HTML attribute array javascript

  20. 20

    using LINQ to get a particular set of objects from a list of objects

  21. 21

    Obtaining lines from an array and stopping at a particular character

  22. 22

    How to find the random value for a particular string in an array

  23. 23

    How can I convert an array of arrays to an array of objects, selecting the key from other array?

  24. 24

    How to get a the sum of multiple arrays within an array of objects?

  25. 25

    How to get only one value in Javascript array of objects using for of and for in statements?

  26. 26

    How to get matching elements from array and change array object?

  27. 27

    How to get the keys as string array from map?

  28. 28

    How to get numeric value from cURL array?

  29. 29

    MongoDB: how to get specific elements from array?

ホットタグ

アーカイブ