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

181
Views
Get the value of an object from the value of another key

I have a Typescript project in which I have two objects. What I am doing is getting a data from the second object depending on the value of the first object.

This is the first object:

let sheet = [
  {
      desc: "work"
  },
  {
      desc: "serv"
  }
]

This is the second object:

let list = [
  {
    "properties": {
      "sheetId": 1000297558,
      "title": "work",
      "index": 0
    }
  },
  {
    "properties": {
      "sheetId": 24134863,
      "title": "serv",
      "index": 1
  }
]

What I want: Get the value of the sheetId property where the value of the title property of that object is equal to the value of the desc property of the first object

This is what I do:

let sheetId: number

for (let getSheet of sheet ) {
  for (let getList of list) {
    if (getList.properties.title == getSheet.desc) {
      sheetId = getList.properties.sheetId
      .
      .
      .
    }
  }
}

My problem: I am iterating twice, each one on an object, this when the process is large consumes a lot, I would like to know if there is another more efficient way to do this

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

0

Convert the list to a Map of sheetId by title O(n) (where n is the length of list), and then extract the sheetId from the Map in O(1).

Basically a Map is an object the hold [key, value] pairs. You can get the value by calling the .get() method with the key. In your case, to prevent iterating the list multiple type, we can iterate it once to index the sheetId by title.

const sheet = [{"desc":"work"},{"desc":"serv"}]

const list = [{"properties":{"sheetId":1000297558,"title":"work","index":0}},{"properties":{"sheetId":24134863,"title":"serv","index":1}}]

const sheetIdByTitle = new Map(list.map(({ properties: p }) => [p.title, p.sheetId]))

console.log(sheetIdByTitle.get(sheet[0].desc))

const ids = sheet.map(o => sheetIdByTitle.get(o.desc))

console.log(ids)

about 4 years ago · Juan Pablo Isaza Report

0

If I understand it correctly, You have both the arrays with same length and in same order. If Yes, you can try this solution.

let sheet = [{
  desc: "work"
}, {
  desc: "serv"
}];

let list = [{
  "properties": {
    "sheetId": 1000297558,
    "title": "work",
    "index": 0
  }
}, {
  "properties": {
    "sheetId": 24134863,
    "title": "serv",
    "index": 1
  }
}];

const res = list.map((obj, index) => obj.properties.title === sheet[index].desc ? obj.properties.sheetId : 'Not matched!');

console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

You can try this variation as well.

let descToSheetIdMap = list.reduce((p, c) => {
  p[c.properties.title] = c.properties.sheetId
  return p
}, {});

for (let getSheet of sheet ) {
  sheetId = descToSheetIdMap[getSheet.desc];
  .
  .
  .
}
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!