1

I Have a JSON input format, here's an exemple :

{
  "friends": [
    {
      "id": "5a8d4euyiuyiuyhiuyc022c7158d5",
      "name": "Gloria Coffey"
    },
    {
      "id": "5a8d4e2rytuyiyuyiytiy3e426",
      "name": "Shawn Ellison"
    }
  ]
}

I would transform it to an array key: value arrays, something like this :

[[id : "5a8d4euyiuyiuyhiuyc022c7158d5", name:"Gloria Coffey"],[id : "5a8d4e2rytuyiyuyiytiy3e426", name:"Shawn Ellison"]]

What I have done :

search(event) {
    this.searchRmpmService.getResults(event.query).then(data => {
    this.results = data.friends;
    console.log(this.results);
    let output = [];
    output= Object.entries(this.results);
    console.log(output);
});

the first console.log of this.results prints me an array of objects

then my output array prints:

0:Array(2)
0:"0" <-- ??
1:{
   id:"5a8d4e2ffead0c022c7158d5",
   name:"Gloria Coffey"
}length:2__proto__:Array(0)

what I would is

id : 5a8d4e2ffead0c022c7158d5
name : Gloria Coffey
5
  • 4
    JavaScript does not have anything that is like [id : "5a8d4euyiuyiuyhiuyc022c7158d5", name:"Gloria Coffey"]. Commented Mar 1, 2018 at 14:56
  • 1
    Javascript does not have key-value arrays, only flat indexed arrays. Your desired result is what you already have at the start. Instead describe the problem you're trying to solve. Commented Mar 1, 2018 at 14:56
  • Looks like that "friends" property already is an array of objects. It's not clear what you're trying to do. Commented Mar 1, 2018 at 14:56
  • 1
    I suggest you take a step back and write in words what steps you need to solve your problem. Pretend that you are teaching another person how to do it. Tell them the steps they need to follow to get the results you want. Write these steps down. After you have a clear idea of the steps required, then you can translate them into JavaScript. Commented Mar 1, 2018 at 14:58
  • You can go with Object.values(data), but also you have to define the case if Object.values(data) is undefined, then you want to return empty array. Commented Mar 1, 2018 at 14:59

2 Answers 2

1

What you are trying to achieve is not possible. The nearest solution of what you want to would be something like this:

let friends = [
    {
      id: "5a8d4euyiuyiuyhiuyc022c7158d5",
      name: "Gloria Coffey"
    },
    {
      id: "5a8d4e2rytuyiyuyiytiy3e426",
      name: "Shawn Ellison"
    }
]

function convert(param) {
  let res = {}
  for (let item of param) {
    let id = item.id
    res[id] = item.name
  }
  return res
}

console.log(convert(friends))

This is not an array, but you can access it like:

let myObj = convert(friends)
console.log(myObj['5a8d4euyiuyiuyhiuyc022c7158d5'])

I hope this will do what you want.

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

Comments

0

You could also do it by using Object.fromEntries() like this:

function flattenArrayToObject(arr) {
  let entries = arr.map(el => [el.id, el.name])
  let obj = Object.fromEntries(entries)
  return obj;
}

Or in a one liner if you really needed to:

let flattenArray = arr => Object.fromEntries(arr.map(el => [el.id, el.name]))

Demo in Stack Snippets

let friends = [
    {id: "158d5", name: "Gloria"},
    {id: "3e426", name: "Shawn"}
]

function flattenArrayToObject(arr) {
  let entries = arr.map(el => [el.id, el.name]) // [["158d5","Gloria"], ["3e426","Shawn"]]
  let obj = Object.fromEntries(entries)
  return obj;
}

console.log(flattenArrayToObject(friends))

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.