Home > Software engineering >  Return object from array of objects
Return object from array of objects

Time:08-27

I have the following array of objects:

const values = [
    {
      clientType: "Client Type 1",
      value: 130
    },
    {
      clientType: "Client Type 2",
      value: 10
    },
    {
      clientType: "Client Type 3",
      value: -80
    },
    {
      clientType: "Client Type 4",
      value: -52
    }
  ]

I want to "map" this array and get as a result the following oject:

results = {
  "Client Type 1": 130,
  "Client Type 2": 10,
  "Client Type 3": -80,
  "Client Type 4": -52,
}

Is there a way of doing this directly? (Using only one map function)

TIA

CodePudding user response:

const values = [
    {
      clientType: "Client Type 1",
      value: 130
    },
    {
      clientType: "Client Type 2",
      value: 10
    },
    {
      clientType: "Client Type 3",
      value: -80
    },
    {
      clientType: "Client Type 4",
      value: -52
    }
  ]
  
const result = values.reduce((acc, {clientType, value}) => ({ ...acc, [clientType]: value}), {})

console.log(result)

CodePudding user response:

This is a fairly simple question/task so I will try to post a simple, easy to understand answer.

const values = [{
      clientType: "Client Type 1",
      value: 130
    },
    {
      clientType: "Client Type 2",
      value: 10
    },
    {
      clientType: "Client Type 3",
      value: -80
    },
    {
      clientType: "Client Type 4",
      value: -52
    }
  ],
  // loop through "values" object and construct and object the way the OP needs then return it.
  resultObj = values.reduce((a, c) => {
    // a: is the object that we are constructing, its default value is {} (empty object)
    // c: is the current object from the "values" array
    a[c.clientType] = c.value;
    return a;
  }, {});

// this line is not needed, it just prints the result to the console
console.log(resultObj);

Just a sidenote (but rather important), the only way to access an attribute on the resulted Object is to use brackets notation: resultObj['Client Type 1'] // prints: 130

Learn more about reduce method on MDN.

CodePudding user response:

This code seems to work:

const values = [
{
  clientType: "Client Type 1",
  value: 130
},
{
  clientType: "Client Type 2",
  value: 10
},
{
  clientType: "Client Type 3",
  value: -80
},
{
  clientType: "Client Type 4",
  value: -52
}
  ]

values.map(getFull);

function getFull(item) {
  return [item.clientType,item.value].join(" ");
}

CodePudding user response:

I hope you will try this code I hope it works for you


const values = [
  {
    clientType: "Client Type 1",
    value: 130,
  },
  {
    clientType: "Client Type 2",
    value: 10,
  },
  {
    clientType: "Client Type 3",
    value: -80,
  },
  {
    clientType: "Client Type 4",
    value: -52,
  },
];

const results = {};
for (let i = 0; i < values.length; i  ) {
  results[values[i].clientType] = values[i].value;
}

console.log("values", values);
// values [
//     { clientType: 'Client Type 1', value: 130 },
//     { clientType: 'Client Type 2', value: 10 },
//     { clientType: 'Client Type 3', value: -80 },
//     { clientType: 'Client Type 4', value: -52 }
//   ]

console.log("results", results);
//   results {
//     'Client Type 1': 130,
//     'Client Type 2': 10,
//     'Client Type 3': -80,
//     'Client Type 4': -52
//   }


  • Related