从数组中提取对象

时间:2020-06-09 21:29:46

标签: javascript arrays json

我要从中映射:

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

对此:

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

我如何实现这一目标: 我有一个选择器,该选择器为我提供了companies对象,然后我将其映射并组装了对象,但我做得不太正确。

这是我的功能:

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)返回以下对象:

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

该函数的结果如下。但是如前所述,我希望它看起来像我帖子的第二个代码部分。

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

3 个答案:

答案 0 :(得分:2)

由于所需的输出是对象而不是数组,因此应使用reduce而不是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);

(我认为您的公司对象中存在语法错误,我已在代码段中进行了纠正)

答案 1 :(得分:1)

您还可以先提取entries个对象,然后再map个对象。

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));

答案 2 :(得分:0)

如果您想尝试新的东西,这是使用我编写的库来表达所需转换的另一种方法。

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>