0

I have an array of objects, and I want to get the name of the cars with the same id and display their name and year like this:

ferrai 01-02-2000, 10-23-2010

lambo 08-12-2018, 02-02-2012

NOT like this:

ferrai 01-02-2000

ferrai 10-23-2010

lambo 08-12-2018

lambo 02-02-2012

cars = [

[0] : { year: "01-02-2000"
 sport:{id: "1111",  name: "ferrai"}

}

[1] : { year: "10-23-2010"
 sport:{id: "1111", name: "ferrari"}

}

[2] : {year: "08-12-2018"
 sport:{id: "2222", name: "lambo"}

}

[3] : {year: "02-02-2012"
 sport:{id: "2222", name: "lambo"}

}

]

how would I be able to do this in angular2+? I have tried this code:

.html file

<div *ngFor="let obj of cars">
  <div *ngFor="let name of obj.sport">
   <div>{{getCars(name)}}</div>
  </div>
</div> 

.ts file

getCars(obj){
var sport = obj.sport;
var year = obj.year;

//do stuff

}

I appreciate any help!

3
  • 1
    jason post your json Commented Sep 23, 2017 at 2:07
  • 1
    you should consider massaging the data a little first before trying to render. It may be cleaner to go through your array and combine all cars with the same name value first, and maybe the years into an array, then when you iterate through you'll have a nice data structure to work with. Commented Sep 23, 2017 at 2:13
  • well I created this cause I thought it would be easier to solve. The real Json is in my other question that I asked a couple days ago from "get ids from json array", but i want the same result to display. Commented Sep 23, 2017 at 2:15

2 Answers 2

1

That answer with reduce is beautiful, and I recommend learning that solution because .reduce is one of the most powerful and versatile methods in native JS.

Here is a more vanilla solution, without using high order functions:

var cars = [
  {
    year: "01-02-2000",
    sport:{id: "1111",  name: "ferrari"}

  },
  {
    year: "10-23-2010",
    sport:{id: "1111", name: "ferrari"}
  },
  {
    year: "08-12-2018",
    sport:{id: "2222", name: "lambo"}
  },
  {
    year: "02-02-2012",
    sport:{id: "2222", name: "lambo"}
  }
]


function simplifyInventory(cars) {
var objList = {};
for (var i = 0; i < cars.length; i++) {
  if (objList.hasOwnProperty(cars[i].sport.name) === false) {
    objList[cars[i].sport.name] = [cars[i].year];
  } else {
    objList[cars[i].sport.name].push(cars[i].year)
  }
}
return objList;
}

console.log(simplifyInventory(cars));

=====console=====

{ ferrari: [ '01-02-2000', '10-23-2010' ], lambo: [ '08-12-2018', '02-02-2012' ] }

Then you could easily put this into almost any form.

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

1 Comment

thank you guys! i've been trying to figure this out for days.
1

I personally would "massage" the data first before having the DOM render things. I personally like to do a lot of the work to the data (either on the server side or before it reaches some DOM iterator to build elements) so that the view can be as simple as possible. Here's what I would do (fixed a typo and this is vanilla javascript):

cars = [
  {
    year: "01-02-2000",
    sport:{id: "1111",  name: "ferrari"}

  },
  {
    year: "10-23-2010",
    sport:{id: "1111", name: "ferrari"}
  },
  {
    year: "08-12-2018",
    sport:{id: "2222", name: "lambo"}
  },
  {
    year: "02-02-2012",
    sport:{id: "2222", name: "lambo"}
  }
]

newCarsData = cars.reduce((acc, curr) => {
  if (!acc.hasOwnProperty(curr.sport.name)) {
    acc[curr.sport.name] = {};
    acc[curr.sport.name].years = []
  }

  acc[curr.sport.name].years.push(curr.year);
  return acc;
}, {});

console.log(newCarsData);

Now you'll have this data structure:

{
  ferrari: { 
    years: [ '01-02-2000', '10-23-2010' ]
  },
  lambo: {
    years: [ '08-12-2018', '02-02-2012' ]
  }
}

and that's easy for a repeat in angular or any framework

1 Comment

Although, now the car names are key names... so you aren't working with an array (at the top level) anymore... lmk if this doesn't work for you

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.