0

I have object with keys and i want to add only specific value to array. for example given keys [3248647, 32486481]

{
  "32486479": {
    "id": "32486479",
    "lat": 33,
    "lng": 73,
    "accuracy": 51,
    "updated_at": 1570908416
  },
  "32486480": {
    "id": "32486480",
    "lat": 33,
    "lng": 73,
    "accuracy": 51,
    "updated_at": 1570908416
  },
  "32486481": {
    "id": "32486481",
    "lat": 33,
    "lng": 73,
    "accuracy": 51,
    "updated_at": 1570908416
  },

Expected result should be

[{
    "id": "3248647",
    "lat": 33,
    "lng": 73,
    "accuracy": 51,
    "updated_at": 1570908416,
},{
    "id": "32486481",
    "lat": 33,
    "lng": 73,
    "accuracy": 51,
    "updated_at": 1570908416
  }]

3 Answers 3

3

keys.map(k => obj[k])

const keys = [32486479, 32486481];

const obj = { "32486479": { "id": "32486479", "lat": 33, "lng": 73, "accuracy": 51, "updated_at": 1570908416 }, "32486480": { "id": "32486480", "lat": 33, "lng": 73, "accuracy": 51, "updated_at": 1570908416 }, "32486481": { "id": "32486481", "lat": 33, "lng": 73, "accuracy": 51, "updated_at": 1570908416 }};

console.log(keys.map(k => obj[k]))

Sign up to request clarification or add additional context in comments.

Comments

2

You could do this using the Array .map method:

const ids = [32486479, 32486481]
const data = {
  "32486479": {
    "id": "32486479",
    "lat": 33,
    "lng": 73,
    "accuracy": 51,
    "updated_at": 1570908416
  },
  "32486480": {
    "id": "32486480",
    "lat": 33,
    "lng": 73,
    "accuracy": 51,
    "updated_at": 1570908416
  },
  "32486481": {
    "id": "32486481",
    "lat": 33,
    "lng": 73,
    "accuracy": 51,
    "updated_at": 1570908416
  }
}

const expectedResult = [{
    "id": "32486479",
    "lat": 33,
    "lng": 73,
    "accuracy": 51,
    "updated_at": 1570908416,
},{
    "id": "32486481",
    "lat": 33,
    "lng": 73,
    "accuracy": 51,
    "updated_at": 1570908416
  }]
  
 const actualResult = ids
   // loop on the ids array and use them to access object inside data
   .map(id => data[String(id)])
   // filter falsy value
   .filter(Boolean);
 
 console.log(actualResult);

Comments

0

Note: using Array.map(e) directly is fine as long as all keys does have a corresponding object as value in the data otherwise it would return undefined for the keys not found and result would have undefined at places.

Better: One answer does solve that by chaining .filter for falsey values.

Another solution: for...of on the keys array to find and push into result.

const keysArr = [3248647, 32486481];
let result = [];

const data = {
  "32486479": {
    id: "32486479",
    lat: 33,
    lng: 73,
    accuracy: 51,
    updated_at: 1570908416
  },
  "32486480": {
    id: "32486480",
    lat: 33,
    lng: 73,
    accuracy: 51,
    updated_at: 1570908416
  },
  "32486481": {
    id: "32486481",
    lat: 33,
    lng: 73,
    accuracy: 51,
    updated_at: 1570908416
  }
};

for (const key of keysArr) {
  if (data[key]) {
    result.push(data[key]);
  }
}

console.info("RESULT::", result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.