使用lodash将相同的键转换为一个并创建其值的数组

时间:2018-02-27 10:24:16

标签: javascript arrays ecmascript-6

我想转换belo input.json文件。我想将所有类别键合并为一个并创建名称数组(如果匹配)。

我正在寻找仅使用lodash或仅使用JS / ES *

的解决方案

已经有类似的问题,但大多数都涉及两个不同的json。请详细说明解决方案

[
    {
        "category": "Salman Khan",
        "names": "Bhaijan"
    },
    {
        "category": "Salman Khan",
        "names": "Sallu"
    },
    {
        "category": "Salman Khan",
        "names": "Dabangg Khan"
    },
    {
        "category": "Salman Khan",
        "names": "Sultan"
    },
    {
        "category": "Shahrukh Khan",
        "names": "SRK"
    },
    {
        "category": "Shahrukh Khan",
        "names": "King Khan"
    },
    {
        "category": "Akshay Kumar",
        "names": "Khiladi"
    },
    {
        "category": "Akshay Kumar",
        "names": "Akki"
    },
    {
        "category": "Aamir Khan",
        "names": "A.K."
    },
    {
        "category": "Aamir Khan",
        "names": "Mr. Perfectionist"
    },
    {
        "category": "Priyanka Chopra",
        "names": "Piggy Chops"
    },
    {
        "category": "Kareena Kapoor",
        "names": "Bebo"
    }
]

expected json

[
    {
        "category": "Salman Khan",
        "names": ["Bhaijan", "Sallu", "Dabangg Khan", "Sultan"]
    },
    {
        "category": "Shahrukh Khan",
        "names": ["SRK", "King Khan"]
    },
    {
        "category": "Akshay Kumar",
        "names": ["Khiladi", "Akki", "A.K."]
    },
    {
        "category": "Aamir Khan",
        "names": ["Mr. Perfectionist"]
    },
    {
        "category": "Priyanka Chopra",
        "names": ["Piggy Chops"]
    },
    {
        "category": "Kareena Kapoor",
        "names": ["Bebo"]
    }
]

3 个答案:

答案 0 :(得分:3)

使用Array#reduce函数迭代数组,并检查当前项目名称是否已存在对象。如果是,只需将名称推入其中,如果没有使用当前项目名称创建新对象。



const data = [
    {
        "category": "Salman Khan",
        "names": "Bhaijan"
    },
    {
        "category": "Salman Khan",
        "names": "Sallu"
    },
    {
        "category": "Salman Khan",
        "names": "Dabangg Khan"
    },
    {
        "category": "Salman Khan",
        "names": "Sultan"
    },
    {
        "category": "Shahrukh Khan",
        "names": "SRK"
    },
    {
        "category": "Shahrukh Khan",
        "names": "King Khan"
    },
    {
        "category": "Akshay Kumar",
        "names": "Khiladi"
    },
    {
        "category": "Akshay Kumar",
        "names": "Akki"
    },
    {
        "category": "Aamir Khan",
        "names": "A.K."
    },
    {
        "category": "Aamir Khan",
        "names": "Mr. Perfectionist"
    },
    {
        "category": "Priyanka Chopra",
        "names": "Piggy Chops"
    },
    {
        "category": "Kareena Kapoor",
        "names": "Bebo"
    }
]

const arr = data.reduce((acc, item) => {
  
  const found = acc.find(i => i.category === item.category);
  
  if(found) {
     found.names.push(item.names);
  } else {
     acc.push({category: item.category, names: [item.names]})
  }
  
  return acc;
}, []);

console.log(arr);




答案 1 :(得分:0)

您可以使用lodash#groupBycategory键对集合中的每个对象进行分组,然后lodash#map将每个组转换为您喜欢的数组格式。

var result = _(data)
  .groupBy('category')
  .map(function(group, category) {
    return {
      category: category,
      names: _.map(group, 'names')
    };
  })
  .value();



var data = [
    {
        "category": "Salman Khan",
        "names": "Bhaijan"
    },
    {
        "category": "Salman Khan",
        "names": "Sallu"
    },
    {
        "category": "Salman Khan",
        "names": "Dabangg Khan"
    },
    {
        "category": "Salman Khan",
        "names": "Sultan"
    },
    {
        "category": "Shahrukh Khan",
        "names": "SRK"
    },
    {
        "category": "Shahrukh Khan",
        "names": "King Khan"
    },
    {
        "category": "Akshay Kumar",
        "names": "Khiladi"
    },
    {
        "category": "Akshay Kumar",
        "names": "Akki"
    },
    {
        "category": "Aamir Khan",
        "names": "A.K."
    },
    {
        "category": "Aamir Khan",
        "names": "Mr. Perfectionist"
    },
    {
        "category": "Priyanka Chopra",
        "names": "Piggy Chops"
    },
    {
        "category": "Kareena Kapoor",
        "names": "Bebo"
    }
];

var result = _(data)
  .groupBy('category')
  .map(function(group, category) {
    return {
      category: category,
      names: _.map(group, 'names')
    };
  })
  .value();

console.log(result);

.as-console-wrapper{min-height:100%;top:0}

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

以下是使用reduce方法的可能解决方案。

const data = [
    {
        "category": "Salman Khan",
        "names": "Bhaijan"
    },
    {
        "category": "Salman Khan",
        "names": "Sallu"
    },
    {
        "category": "Salman Khan",
        "names": "Dabangg Khan"
    },
    {
        "category": "Salman Khan",
        "names": "Sultan"
    },
    {
        "category": "Shahrukh Khan",
        "names": "SRK"
    },
    {
        "category": "Shahrukh Khan",
        "names": "King Khan"
    },
    {
        "category": "Akshay Kumar",
        "names": "Khiladi"
    },
    {
        "category": "Akshay Kumar",
        "names": "Akki"
    },
    {
        "category": "Aamir Khan",
        "names": "A.K."
    },
    {
        "category": "Aamir Khan",
        "names": "Mr. Perfectionist"
    },
    {
        "category": "Priyanka Chopra",
        "names": "Piggy Chops"
    },
    {
        "category": "Kareena Kapoor",
        "names": "Bebo"
    }
];

const resultObject = data.reduce((acc, value) => {
  acc[value.category] = acc[value.category] || [];
  acc[value.category].push(value.names);
  return acc; 
},{});

const results = Object.keys(resultObject).map(key => ({ category: key, names: resultObject[key]}));

console.log(results);