Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
how to get the value by the help of includes/index of

how to get the value in any way if key does match with "Item"

   const data = [{
            "Item-55566": "phone",        
        },
        {
            "Items-44555": "Case",       
        }
    ];
    
    /* How to get value if index found by Item */
    
    for(let i = 0; i<data.length; i++) {
        console.log(data[i].includes("Item"));
        //Expecting phone and case
    }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

for-in allows you to loop through the keys in an object. Not to be confused with for-of, which loop through elements in an array.

const data = [{
          "Item-55566": "phone",        
      },
      {
          "Items-44555": "Case",       
      }
 ];
 
for(let datum of data)
{
  for(let key in datum)
  {
    if(key.includes("Item"))
    {
      console.log(datum[key]);
    }
  }
}

about 4 years ago · Juan Pablo Isaza Report

0

If you want to keep your loop (good that it's only one loop compared to other answers) you can do it with Object.keys and values:

   const data = [{
            "Item-55566": "phone",        
        },
        {
            "Items-44555": "Case",       
        }
    ];
    
    /* How to get value if index found by Item */
    
    for(let i = 0; i<data.length; i++) {
        if(Object.keys(data[i])[0].includes('Item')){
            console.log(Object.values(data[i])[0]);
        }
    }

about 4 years ago · Juan Pablo Isaza Report

0

In the simple way just change data[i].includes("Item") to data[i].keys().includes("Item").

BUT! Could we have some alternative data set here? For example:

const data = [{
        "Item-55566": "phone",
        "SomeKey: "Some Value",
        123123: "Numeric key with value"
    },
    {
        "Items-44555": "Case",
        "Another-key": "Another value"
    }
];

In this case you need to put some changes in your code to find correct keys & values:

for(let i = 0; i<data.length; i++) {
    data[i].keys().forEach(v=>{ 
      String(v).includes("Item") && console.log("Got index: ${i}, key: ${v}, value: ${data[i][v]}") 
    })

}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!