1

This has been posted time and time again and after reading them all, I still don't know what I'm doing wrong. I understand there are so many ways to do this and I get the feeling I've mixed them up.

I'm trying to get the uniques value from serviceType so rather than having Cars Now, Cars Now, Vans Now, I should only have Cars No, Vans Now.

See attached code

const preFilteredRows = [
{
"serviceType": "Cars Now",
"applictionType": "Direct",
"wheelType": "4x4"
},
{
"serviceType": "Cars Now",
"applictionType": "web",
"wheelType": "2x4"
},
,
{
"serviceType": "Vans Now",
"applictionType": "Direct",
"wheelType": "2x4"
}
]

let tempArr = [];
preFilteredRows.forEach(row => {
    if(row.values[serviceType].length == 1) tempArr.push(row.values[serviceType][0])
})
/* return [...new Set(tempArr)] */
newServiceSet = [...new Set(tempArr)]
console.log(newServiceSet)

UPDATE: Thank you all so much for your feedback, I really do appreciate your help with my learning.

One part I forgot to add to my question and just realise is, I not only need the uniques values, but I also need them displayed like the below image so each value has a value and a label. I don't need the count anymore.

enter image description here

3
  • 1
    None of them have the .values your if statement checks for Commented Mar 24, 2022 at 13:22
  • I don't understand what you think you're doing in your forEach(); I think that row.values[serviceType].length == 1 should be row.serviceType.length > 0; but honestly I don't really know (as I said) what you're testing for (and this is why comments are good things). Commented Mar 24, 2022 at 13:25
  • Your updates to the question should be separate question. Please create a separate thread for it. Commented Mar 25, 2022 at 1:14

3 Answers 3

2

If I understand correctly you want

[
  "Cars Now",
  "Vans Now"
]

Do a map and filter and then do your spread operator for unique values.

const preFilteredRows = [
{
"serviceType": "Cars Now",
"applictionType": "Direct",
"wheelType": "4x4"
},
{
"serviceType": "Cars Now",
"applictionType": "web",
"wheelType": "2x4"
},
,
{
"serviceType": "Vans Now",
"applictionType": "Direct",
"wheelType": "2x4"
}
]

let tempArr = preFilteredRows.map(row => row.serviceType) // map extracts the serviceType
           .filter(service => service); // filter removes any undefined/empty values

newServiceSet = [...new Set(tempArr)] // only keeps unique values

console.log(newServiceSet)

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

Comments

0

You can do it like this:

  • map - to map each object from array to its serviceType property value
  • new Set - to get all unique values from the array generated in above step

const preFilteredRows = [
  { "serviceType": "Cars Now", "applictionType": "Direct", "wheelType": "4x4" },
  { "serviceType": "Cars Now", "applictionType": "web", "wheelType": "2x4" },
  { "serviceType": "Vans Now", "applictionType": "Direct", "wheelType": "2x4" }
];

const unique_types = [...new Set(preFilteredRows.map(item => item.serviceType))];
console.log(unique_types);

Comments

0

A mix of map and filter would be do this job.

const preFilteredRows = [
{
"serviceType": "Cars Now",
"applictionType": "Direct",
"wheelType": "4x4"
},
{
"serviceType": "Cars Now",
"applictionType": "web",
"wheelType": "2x4"
},
,
{
"serviceType": "Vans Now",
"applictionType": "Direct",
"wheelType": "2x4"
}
]
const n = [...new Set( preFilteredRows.map(obj => obj.serviceType)) ].filter(f => f);

console.log(n)

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.