0

I have an object that looks like this:

[{
     "id": 3298,
     "kanji": "籲",
     "part": "一 | 亅 个 ハ 冂 口 目 冊 竹 貝 頁 龠 廾",
     "createdAt": "2018-04-16T02:27:33.993Z",
     "updatedAt": "2018-04-16T02:27:33.993Z"
 }, {
     "id": 5801,
     "kanji": "龥",
     "part": "一 | 亅 个 ハ 冂 口 廾 目 冊 貝 頁 龠",
     "createdAt": "2018-04-16T02:27:34.102Z",
     "updatedAt": "2018-04-16T02:27:34.102Z"
 }]

I would like to combine this with an array that holds unique the kanji and part and remove the duplicate part. I want it to return the result as follows

{
    "kanji": ["籲", "龥"],
    "part": ["一", "|", "亅", "个", "ハ", "冂", "口", "目", "冊", "竹", "貝", "頁", "龠", "廾"]
}

How can I use the lodash to do it?

2
  • Why do you have to put the expected object on an array? Commented Apr 16, 2018 at 6:04
  • 1
    @Eddie I just edited my expected object, thank you for your attention :) Commented Apr 16, 2018 at 6:15

3 Answers 3

1

You can use map with uniq to get all unique value in an object.

const data = [ { "id": 3298, "kanji": "籲", "part": "一 | 亅 个 ハ 冂 口 目 冊 竹 貝 頁 龠 廾", "createdAt": "2018-04-16T02:27:33.993Z", "updatedAt": "2018-04-16T02:27:33.993Z" }, { "id": 5801, "kanji": "龥", "part": "一 | 亅 个 ハ 冂 口 廾 目 冊 貝 頁 龠", "createdAt": "2018-04-16T02:27:34.102Z","updatedAt": "2018-04-16T02:27:34.102Z" } ];
const result = {
      kanji : _(data).map('kanji').uniq().value(),
      part : _(data).map('part').split(' ').uniq().value()
     };
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>

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

Comments

0

You can use flatMap to get a flattened array for kanjis and parts. Then use uniq to only retain unique elements:

const data = [
  {
    "id": 3298,
    "kanji": "籲",
    "part": "一 | 亅 个 ハ 冂 口 目 冊 竹 貝 頁 龠 廾",
    "createdAt": "2018-04-16T02:27:33.993Z",
    "updatedAt": "2018-04-16T02:27:33.993Z"
  },
  {
    "id": 5801,
    "kanji": "龥",
    "part": "一 | 亅 个 ハ 冂 口 廾 目 冊 貝 頁 龠",
    "createdAt": "2018-04-16T02:27:34.102Z",
    "updatedAt": "2018-04-16T02:27:34.102Z"
  }
];

const result = {
  kanji: _.uniq(_.flatMap(data, 'kanji')),
  part: _.uniq(_.flatMap(data, p => p.part.split(' ')))
};

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>

Comments

0

You can also use reduce() and the spread operator, you can achieve your required result.

DEMO

const arr=[
  {
    "id": 3298,
    "kanji": "籲",
    "part": "一 | 亅 个 ハ 冂 口 目 冊 竹 貝 頁 龠 廾",
    "createdAt": "2018-04-16T02:27:33.993Z",
    "updatedAt": "2018-04-16T02:27:33.993Z"
  },
  {
    "id": 5801,
    "kanji": "龥",
    "part": "一 | 亅 个 ハ 冂 口 廾 目 冊 貝 頁 龠",
    "createdAt": "2018-04-16T02:27:34.102Z",
    "updatedAt": "2018-04-16T02:27:34.102Z"
  }
]

let result= arr.reduce((o,v)=>{
	o.kanji.push(v.kanji);
	o.part=[...o.part,...v.part.split(' ')];
	return o;
},{kanji:[],part:[]});

console.log(result);

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.