0

I have to write map function based on response data but response data is not an array

example response

{
  "data": {
    "e680c823-895b-46f0-b0a0-5f8c7ce57cb2": {
      "id": 98218,
       ......
    },
    "e24ed385-0b86-422e-a4cc-69064846e13b": {
      "id": 98217,
       .......
    },
    "c1cc583b-a2be-412b-a286-436563984685": {
      "id": 118868,
      .....
    }, 
}

var posts = data.map(function (item) {
    return  item.id;
});

I have to achieve like this console

console.log( posts[1].id ) //98217 

2 Answers 2

1

map over the Object.values and return the nested object.

const data = {
  data: {
    "e680c823-895b-46f0-b0a0-5f8c7ce57cb2": {
      "id": 98218
    },
    "e24ed385-0b86-422e-a4cc-69064846e13b": {
      "id": 98217
    },
    "c1cc583b-a2be-412b-a286-436563984685": {
      "id": 118868
    }
  }
};

const posts = Object.values(data.data).map(obj => obj);
console.log(posts[1].id);

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

Comments

1

Object.values() returns an array of Object values. You can loop over them to get your desired result.

let respData = {
  "data": {
    "e680c823-895b-46f0-b0a0-5f8c7ce57cb2": {
      "id": 98218
 
    },
    "e24ed385-0b86-422e-a4cc-69064846e13b": {
      "id": 98217
    },
    "c1cc583b-a2be-412b-a286-436563984685": {
      "id": 118868
    }
}
}


var posts = Object.values(respData['data']).map(function (item) {
    return  item;
});

console.log(posts[0].id) //98218

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.