0

Basically I have an array, like this:

const companies = [
  {
    name: "Company One",
    category: "Finance, Finance, Technology, Auto, Same, Same, same",
    start: 1981,
    end: 2004
  }
]

In category, I want to write a .map or if statement to find the value matches the value if so remove all the extra values (Same for example) and just leave one instance of it.

So far what I have done is this:

const newSingles = companies.map(function(company) {
  if (company.category === company.category) {
    console.log("cats match remove and leave one");
    //This is where I am stuck?!
  };
});

It's driving me a little crazy as I was going to use .pop() but not sure how to. What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Your if statement is not clear, what do you want to check? – zb22 May 10 '20 at 18:27
  • Try this [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – 54ka May 10 '20 at 18:27
  • Hi Bob. I see that you posted a question in duplicate today, and then deleted both copies (and a Reddit account) at the first sign of a road-bump. Don't do this please - accepting reasonable criticism is part and parcel of posting on the internet. – halfer Jun 14 '20 at 11:32
  • There is a generally a view that questions should not be posted in multiple places at one time, since scattergun approaches say to community members "I am just driving by, looking for free help, don't plan to stay", and some folks don't see that as part of the normal give-and-take of community. We also find that, where "cross posting" happens, it gets solved in one location, leaving other people to waste their time providing help on the other copies (I've lost count of the number of times I have seen that). – halfer Jun 14 '20 at 11:34
  • Thus, at the very least, it is a kindness to link all copies to all copies, so readers who can see any copy can check all other copies for what has already been said. The litmus test here is - is the question author taking care not to waste the time of volunteers? If a good effort is made, people will appreciate it. – halfer Jun 14 '20 at 11:35

3 Answers3

1

So thank you for all the help everybody, it is very kind to get help!

If I put all my categories in their own array:

const companies = [
  {
    name: "Company One",
    category: ["Finance", "Finance", "Technology", "Auto", "Same",
               "Same"],
    start: 1981,
    end: 2004
  }
]; 

Then do this:

companies.map(it => 
    it.category = it.category.reduce((previous, currItem) => {
        if (!previous.find(
                    x => x.toLowerCase() === currItem.toLowerCase()))
            previous.push(currItem);
        return previous;
    }, []));

That gives me an output of the following:

[
  {
    name: 'Company One',
    category: [ 'Finance', 'Technology', 'Auto', 'Same' ],
    start: 1981,
    end: 2004
  }
]

Again thank your for your help :)

0

Try Array.filter(), do not use an if statement inside .map()

Maybe this will help you: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

0

You should split your category and you need to find the unique values in your category array list. Try something like;

var category = ["Finance", "Finance", "Technology", "Auto", "Same", "Same", "same" ];
var uniques= category.filter((item, i, ar) => ar.indexOf(item) === i);

console.log(uniques);

With a little change, your new implementation would be something like this;

const newSingles = companies.map(function(company) {
const category = company.category.filter((item, i, ar) => ar.indexOf(item) === i);
  return {
   ...company,
   category
  }
});

And here is your new result;

[
  {
    name: "Company One",
    category:[ "Finance", "Technology", "Auto", "Same", "same"],
    start: 1981,
    end: 2004
  }
]
sulhadin
  • 521
  • 4
  • 16