1

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

2
  • What have you tried and what output did you get? Commented Aug 26, 2022 at 16:29
  • Before asking the question here, I could not get it to work. I did not know that in order to get the attribute of an object I had to use bracket notation. Not much JS experience here. The answers here were a real class to me. Thanks. Commented Aug 26, 2022 at 22:47

4 Answers 4

4

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)

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

2 Comments

No offense but it seems the output of your attempt is different from the output the OP wants.
@ths thanks for notifying me, I just update the code.
2

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.

Comments

0

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(" ");
}

Comments

0

Try this:

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
//   }

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.