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

215
Views
Extracting values from a Javascript Object

I am trying o get data from SWAPI to integrate with another platform using javascript. I have some saved some links inside a variable while getting data.

a = {
  items: [
    { item: "https://swapi.dev/api/people/5/" },
    { item: "https://swapi.dev/api/people/68/" },
    { item: "https://swapi.dev/api/people/81/" },
  ],
};

From this code how do I extract the links only and save in a variable? I only need links. I tried object.values, object.keys() but couldn't find a way. The platform I am using doesn't support fetch() or $.each.

I am not quite sure how to get it done using for loop. Any assistance will be highly appreciated!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use a simple forEach for this task like:

const a = {
  "items": [{
      "item": "https://swapi.dev/api/people/5/"
    }, {
      "item": "https://swapi.dev/api/people/68/"
    },
    {
      "item": "https://swapi.dev/api/people/81/"
    }
  ]
};

a.items.forEach(el =>{
  console.log(el.item);
});

Reference:

  • Array.prototype.forEach()
about 4 years ago · Juan Pablo Isaza Report

0

There are many different ways to achieve this, a few already suggested.

Using a for loop:

// Create an empty array to store the values
const linksArray = [];

// Loop the object items array to get its values
for (let i = 0; i < a.items.length; i++) {
  // Store the values in the empty array
  linksArray.push(a.items[i].item);
}

// See the results
console.log(linksArray)
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!