0

I want to map from this:

companies: {
  apples: {
   Q7: {
    price: 1,
   },
   Q6: {
    price: 1,
   },
  peaches: {
   Q7: {
    price: 1,
   },
   Q6: {
    price: 1,
   },
 },
};

to this:

{ "companies": {
    "apples": [
        {
            "name": "Q7",
            "price": 1
        },{
            "name": "Q6",
            "price": 1
        }
    ],
    "peaches": [
        {
            "name": "Q7",
            "price": 1
        },{
            "name": "Q6",
            "price": 1
        }
    ]
  }
}

How I am trying to achieve this: I have a selector which gives me the companies object and then I map over it and assemble my object but I don't get it quite right.

This is my function:

const weaponProducts = Object.entries(getCompanies(state)).map(([companyType, companies]) => {
        const prod = Object.entries(companies).map(([key, companies]) => {
           return {
               name: key,
               price: companies.price
           }
        });
        return {
            [companyType]: prod
        };
    });

getCompanies(state) returns the following object:

{
    "companies": {
        "apples": {
            "Q7": {
                "price": 1
            },
            "Q6": {
                "price": 1
            }
        },
        "peaches": {
            "Q7": {
                "price": 1
            },
            "Q6": {
                "price": 1
            }
        }
    }
}

The result of the function is the following. But as explained I want it to look like the second code section of my post.

[
  {
    "apples": [
      {
        "name": "Q7",
        "price": 1
      },
      {
        "name": "Q6",
        "price": 1
      },
    ]
  },
  {
    "peaches": [
      {
        "name": "Q7",
        "price": 1
      },
      {
        "name": "Q6",
        "price": 1
      },
    ]
  }
]
7
  • 1
    Actually if the result shown here is the result of the function, it works correctly. The second companies which u asked to create will break the code cause there is an object without key and with values. I'm not sure if this make sense for you Commented Jun 9, 2020 at 21:37
  • I edited the post above. The last code section of my post is the actual return value of the function. But I dont want it to be like that. Can you please tell me what to adjust in my function in order to make it look like in the second code section. Commented Jun 9, 2020 at 21:43
  • your second output doesn't look like valid JSON Commented Jun 9, 2020 at 22:00
  • Yea sure just first can you post what this Object.entries(getCompanies(state)) gives you back? Commented Jun 9, 2020 at 22:00
  • This is the output of the function Halil: jsonblob.com/64de82a7-aa9e-11ea-a88a-4ff68eaea4eb Commented Jun 9, 2020 at 22:12

3 Answers 3

2

since your desired output is an object, not an array, you should use reduce instead of map:

let companies = {
  apples: {
   Q7: {
    price: 1,
   },
   Q6: {
    price: 1,
   },
  },
  peaches: {
   Q7: {
    price: 1,
   },
   Q6: {
    price: 1,
   },
 },
}

let fruit = Object.keys(companies)
let output = fruit.reduce((output, currentFruit) => {
  output[currentFruit] = Object.keys(companies[currentFruit]).map(q => {
    return { name: q, price: companies[currentFruit][q].price }
  })
  return output
}, {});

console.log(output);

(I think there was a syntax error in your companies object, I corrected in the snippet)

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

Comments

1

You can also take entries and then map the objects accordingly.

var companies = { apples: { Q7: { price: 1, }, Q6: { price: 1, }, }, peaches: { Q7: { price: 1, }, Q6: { price: 1, } }};

const result = (inputObj) =>
  Object.fromEntries(
    Object.entries(inputObj).map(([key, obj]) => [
      key,
      Object.entries(obj).map(([name, val]) => ({ name, ...val })),
    ])
  );

console.log(result(companies));

1 Comment

This is even a more elegant solution. Thanks!
0

If you're down to try something new, here's a different way to express the desired transformation using a library I authored.

const x = { companies: { apples: { Q7: { price: 1, }, Q6: { price: 1, }, }, peaches: { Q7: { price: 1, }, Q6: { price: 1, }, }, } }

const { pipe, fork, map, tap, get } = rubico

const y = map(map(pipe([
  Object.entries,
  map(fork({
    name: get(0),
    price: get([1, 'price']),
  })),
])))(x)

console.log(JSON.stringify(y, null, 2))
<script src="https://unpkg.com/rubico/index.js"></script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.