0

I have the following json data and want to extract unique vendor fields into an array. How do I accomplish this in angular2 component.

Here is my data:

[{"category": "Living Room", "vendor": "Flexsteel"},
{"category": "Living Room", "vendor": "Klaussner"},
{"category": "Living Room", "vendor": "Flexsteel"},
{"category": "Living Room", "vendor": "Craftmaster"},
{"category": "Living Room", "vendor": "Craftmaster"}]

and I want the resulting array to show:

[{"vendor": "Flexsteel"},
{"vendor": "Klaussner"},
{"vendor": "Craftmaster"}]

3 Answers 3

2

Try this:

const data = [...your data...];

const vendors = data.map(d => {
  return { vendor: d.vendor };
});
Sign up to request clarification or add additional context in comments.

4 Comments

I am getting a error on "vendor" where you have {vendor: d.vendor}. It is saying "unused label". I am not sure what that means.
Oh, sotty...brackets for an object in lambda expressions...no good idea -> Updated answer
This works. Is there a direct way to extract unique vendors or do I have to write a function to do this?
const unique = [...Array.from(new Set(vendors.map(v => v.vendor)))];
2

You can just use Array.map:

let data = [
        {"category": "Living Room", "vendor": "Flexsteel"},
        {"category": "Living Room", "vendor": "Klaussner"},
        {"category": "Living Room", "vendor": "Flexsteel"},
        {"category": "Living Room", "vendor": "Craftmaster"}
      ];
let processedData = data.map(dat => ({ vendor: dat.vendor }));

Comments

2

First you need to get the vendors, you can get it like this:

let data = [{"category": "Living Room", "vendor": "Flexsteel"},
            {"category": "Living Room", "vendor": "Klaussner"},
            {"category": "Living Room", "vendor": "Flexsteel"},
            {"category": "Living Room", "vendor": "Craftmaster"}];

// Get all the vendors 
let vendors = data.map(dat => ({ vendor: dat.vendor }));

After that you'll need to filter the vendors and get only unique vendors using:

// Unique Vendors 
let uniqueVendors= Array.from(new Set(vendors.map((x) => JSON.stringify(x))))
                                             .map((y) => JSON.parse(y.toString()));

Full code snippet:

let data = [{"category": "Living Room", "vendor": "Flexsteel"},
            {"category": "Living Room", "vendor": "Klaussner"},
            {"category": "Living Room", "vendor": "Flexsteel"},
            {"category": "Living Room", "vendor": "Craftmaster"}];

// Get all the vendors 
let vendors = data.map(dat => ({ vendor: dat.vendor }));

let uniqueVendors= Array.from(new Set(vendors.map((x) => JSON.stringify(x))))
                                             .map((y) => JSON.parse(y.toString()));
                                             
console.log(uniqueVendors)

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.