Javascript newbie question

Very new to this stuff, so please bear with me…

Let’s say I have an array like this:

const array1 = [ 
    { Temp: 1525, DewPt: 69, CP: 0.30 },
    { Temp: 1525, DewPt: 64, CP: 0.35 },
    { Temp: 1525, DewPt: 60, CP: 0.40 },
    { Temp: 1525, DewPt: 57, CP: 0.45 },
    { Temp: 1525, DewPt: 53, CP: 0.50 },
    { Temp: 1525, DewPt: 50, CP: 0.55 },
    { Temp: 1525, DewPt: 48, CP: 0.60 },
    { Temp: 1525, DewPt: 45, CP: 0.65 }
];

I want to check the first two values (Temp and DewPt) and if both match, return the CP value. From my searching, it would appear the array.includes function might be a starting point, so I tried demo’ing something like this:

console.log(array1.includes(1525 && 69));

just to see if I could get a TRUE return, but could not even get that (probably bad syntax or something).

Thanks in advance for anyone’s help.

2 Likes

It looks like you have an array of objects.
As far as I know you would need to create a custom function to search this array in order to find the specific object that has the specific property values you are searching on.

Edit: read here for more info on this

1 Like

that is something incorrect.

You have array of objects

And you need to check some properties of these objects.

Try to add console.log stuff like that, for example

console.log(array1[0].Temp)

Study output, that should give you some idea how to access data you need.

JS course in the curriculum has some lessons how to deal with array of objects or somewhat similar data structures, I don’t remember exact steps tho.

If I would follow advice from @hbar1st , I would create a function, with this logic:

const giveMeCPplease = (temp, dewpt, arr) => {
  // loop through given array
  for (let item of arr) {
    // here goes comparison checks, for temp and dewpt
    // when you have a match:
    // return cp
  }
}

Firstly, ur syntax and structure is wrong. U should always use Square brackets to construct ur array as it’s conventional in Javascript programming language.
Secondly, ur sub arrays should have ids for it to be readable and understandable. This is what i mean:
const MY_OBJ=[
2022:[
“title”: “Love”;
“song”: “Man”;
],
2023:[

]
];

the years here are d ids for each sub array.
Thus if i want to access 2022 and “title” , i could do it like this:
console.lo(MY_OBJ.[0].[0]);

That’s my own opinion.
Lastly, work harder!

function checkTemp(arr, target) {
    for (let i = 0; i < arr.length; i++) {
        const obj = arr[i];
        if (obj.Temp === target.Temp &&I obj.DewPt === target.DewPt) {
            return obj.CP;
        }
    }
}

checkTemp(array1, { Temp: 1525, DewPt: 69 }); // returns 0.3

This reference can be helpful for new learners. It provides a list of JavaScript questions that can help them test their knowledge and improve their language understanding.

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