Getting specific value from key. Array Object

I have an object:

const car = [{ year: "2000", model: "500", color: "white" }, { year: "2020", model: "500", color: "white" }];

When I add the brackets around the object I can not seem to grab the year. If I remove the brackets I can access the year value by writing:

var a = car.year;
console.log(a); // 2000, 2020

I think this is a syntax issue on my part but haven’t been able to figure out how to get the value only for each object.

Is there a way that for each loop you can store the year for each object in the array in a variable? Again, this works if I only have one object. But if I add multiple objects to the car variable and add brackets, I can no longer access the value by using

car.year;

When you have them in an array, forEach has each object as the first function parameter.

I find that it helps to use plural for arrays, so that the singular term can be used without confusion as the loop parameter. Here, that means using plural cars and singular car.

const cars = [
  {year: "2000", model: "500", color: "white"},
  {year: "2020", model: "500", color: "white"}
];

cars.forEach(function (car) {
  console.log(car.year); // 2000, 2020
});

There might be a communication confusion going on here, so can you please supply some example code of the problem you are facing?

1 Like

I get undefined when I change the car variable to cars. Here’s sample code: Pulled some from the SitePoint blog post. Call JS function string without using eval Essentially, I am storing a function name in the car object and then am needing to extract the string which is then used in a switch statement to determine which function to call. So if there are 5 different objects then there would be 5 function calls using the year value for each.

    function extractYearValue(car) {
        for (const [key, value] of Object.entries(car)) {
            var fnstring = car.year;
            switch (fnstring) {
                case "2020": carFuncA(); break;
                case "2000": carFuncB(); break;
            }
        }
    }

Well in that function there’s no good reason to use cars instead of car, because that whole function relates to a single car whenever it’s being used.

Right.

Is there a correct way to grab the value for each object when their are multiple objects?

As mentioned before car.year works if there’s one object but not if there’s multiple objects.

I’ve tried several ways [car.year] [car(year)] etc. can’t seem to figure it out.

Can you please supply some example objects that are the multiple ones that you are having trouble with?

This is my current object.

What I am needing is the first loop to get 2000 and the second loop 2020.

The code that I gave in my first post seems to achieve what you are asking.

Here is a sample page where I have output each year from your object to the page.

1 Like

And, parameter destructuring (also called parameter context matching) can be used, so that we can more explicitly state that we want only certain values from the object being given to the function.

// cars.forEach(function (car) {
cars.forEach(function ({year, model}) {
  document.body.innerHTML += "<p>Year: " + year + ", Model; " + model + "</p>";
});
1 Like

Thank you. Had to wrap it in a function so it would wait until called but it works. Thank you!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.