1

I'd appreciate any advice on the below. I need to extract the keywords from the array within an array of objects below, and reduce them to show all keywords without repetition.

My data JSON object is below:

[
  {
    "word":"Cat",
    "answer":"A type of feline",
    "keywords": ["pet", "mouse-catcher"]
  },
  {
    "word":"Dog",
    "answer":"A type of canine",
    "keywords": ["pet", "cat-catcher"]
  },
]

My JS code is below:

let keywordList = data.map(entry => {

  let list = [...entry.keywords];
    return (
      list.reduce(( finalArray, current ) => finalArray.concat(current),[])
  );
});

Within my React component, I iterate over the array using map again:

<p>
  keywords: {keywordList.map((word, index) => {
    return (
      <span key={word+index}>
        <a onClick={this.searchKeyword} href="#" id={word}>{word}</a>
        <span> | </span>
      </span>
    );

  })}
</p>

Unfortunately, my reduce function doesn't seem to be working, I'm getting an array of arrays. Any advice would be great.

1
  • Yes, you're clearly creating an array of arrays. Why don't you show us what the data should look like? Commented Aug 20, 2017 at 18:45

3 Answers 3

2
let data = [{
    "word": "Cat",
    "answer": "A type of feline",
    "keywords": ["pet", "mouse-catcher"]
  }, {
    "word": "Dog",
    "answer": "A type of canine",
    "keywords": ["pet", "cat-catcher"]
  }
]

let keywords = [...new Set(data.reduce((a, e) => a.concat(e.keywords), []))]

JSFiddle

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

3 Comments

Thanks, this is exactly what I need, although all the answers here are great, and work perfectly! I need to practice using the reduce function more, thank you!
How do I mark it as answered? I upvoted both correct answered, but my vote is not registered as I don't have enough 'reputation, but I am unaware of how to mark it as answered. Thanks.
Done, thanks, I never noticed the tick mark there before, cheers!
1

Instead of map, a reduce with a filter:

var list = data.reduce((p, c) => {
  return p.concat(c.keywords.filter(el => !p.includes(el)));
}, []);

DEMO

Comments

0
const arr = [
  {
    "word":"Cat",
    "answer":"A type of feline",
    "keywords": ["pet", "mouse-catcher"]
  },
  {
    "word":"Dog",
    "answer":"A type of canine",
    "keywords": ["pet", "cat-catcher"]
  },
]

const keywords = [ ...arr.reduce((keywords,obj)=> [...keywords, ...obj.keywords ], [] )
                  .reduce((set,keyword)=> set.add(keyword), new Set() ) ]

The first reduce merges all keywords with repetition by concatenation and the second reduce converts the array into a Set removing all duplication. Finally, I wrap it with the resulting set with
[ ...Set ] to convert it into an array.

JS Bin

1 Comment

Thanks, all the answers here are great, and much appreciated – although my upvote is not registered as I don't have enough 'reputation'. Thanks very much to all!

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.