getting specific values from objects within arrays

Bruce B

Considering the following array which includes several objects within it, I tried to get specific values in one single array.To put it delicately,I wanted to pull out the name of people who were in the "red" team.

// 
    const array = [
      {
        username: "Mike",
        team: "red",
        score: 20,
     items: ["bat", "book", "gloves"]
  },
  {
    username: "Moe",
    team: "blue",
    score: 30,
    items: ["cellphone", "backpack", "cards"]
  },
  {
    username: "Ellie",
    team: "red",
    score: 15,
    items: ["ball", "canteen", "crayon"]
  },
  {
    username: "little_joe",
    team: "green",
    score: 1,
    items: ["book", "pencil"]
  },

];

//using filter method :

const filterArray=array.filter((num)=>{ if (num.team==="red" ){ return num.username}});
console.log(filterArray);

//result : an array including two objects full of unwanted values.

What should I do to get one single array with only desired value ( not two big objects) ?

I'm trying to get an array like this : (2)►[Mike,Ellie]

Cory Kleiser

One way to do this would be to use Array.prototype.filter and Array.prototype.map. However, the preferred way would be to use Array.prototype.reduce as it will be much more efficient.

Like this:

const array = [{
    username: "Mike",
    team: "red",
    score: 20,
    items: ["bat", "book", "gloves"]
  },
  {
    username: "Moe",
    team: "blue",
    score: 30,
    items: ["cellphone", "backpack", "cards"]
  },
  {
    username: "Ellie",
    team: "red",
    score: 15,
    items: ["ball", "canteen", "crayon"]
  },
  {
    username: "little_joe",
    team: "green",
    score: 1,
    items: ["book", "pencil"]
  },

];

const newArray = array.reduce((n, val) => {
  if(val.team == "red") n.push(val.username);
  return n;
}, []);

console.log(newArray);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get all specific values from an array of objects with nested arrays of objects?

Loop through array of arrays which contain objects and get all specific values from the objects then show to the page JQuery

Getting a specific key from within a hashtable

Getting specific values from connection string

Getting values from multiple JSON objects

Getting access to deep nested arrays within objects

Getting values from array of arrays

Getting specific field values from Json Python

Creating objects from arrays with repeating values

objects in arrays- getting values

Extracting specific values from an array within an object

Getting values from time indexed pandas dataframe for a specific time within the two timestamps

Getting specific values from api response

ObjC – Getting values from Objects in array(NSMutableArray)

Getting specidic values from PHP arrays

Getting unique values within two arrays in one document in MongoDB

Print out specific values from a JSON file (nested objects-arrays) w/ JAVA

Getting a list of values from dictionary when values are tuples within lists?

Getting first values from composite type arrays

Getting specific values from different lists and how to

getting distinct values from array of arrays in Javascript

Getting specific object property values from multiple arrays

Getting Values from comparing Two Arrays

getting values from json objects in reactjs

How to access a specific property within a nested but fixed structure of arrays and objects

Creating new array of objects from specific values in 2 different arrays (JavaScript)

Create new array of arrays from arrays of objects of array , with specific value

How to get array of unique values from arrays within an array of objects?

Compare javascript arrays of objects and set specific values which applies to both additions and deletions from main array source