0

I am filtering a javascript object..

[{
   title: 'example1',
   categorySlug: 'cat-1',
   categoryName: 'hello1'
}
{
   title: 'example2',
   categorySlug: 'cat-2',
   categoryName: 'hello2'
}
{
   title: 'example3',
   categorySlug: 'cat-1',
   categoryName: 'hello1'
}]

I need the object as below

[{
    categorySlug: "cat1",
    categoryName: "hello1",
    data: [{
        {
            title: "example1",
            categorySlug: 'cat-1',
            categoryName: 'hello1'
        },
        {
            title: "example1",
            categorySlug: 'cat-1',
            categoryName: 'hello1'
        }
    }]
},
 {
    categorySlug: "cat2",
    categoryName: "hello2",
    data: [{
        {
            title: "example2",
            categorySlug: 'cat-2',
            categoryName: 'hello2'
        }
    }]
}
]   

I need to filter the object. I have tried with many way but I have faield to filter the object.

#I have added one more category And I nedd to filter like that.

1
  • Is categorySlug and categoryName always the same in pairs? (i.e. cat-2 always together with hello2 etc.) Commented Aug 30, 2022 at 10:22

3 Answers 3

1

You can use the array map method.


const array = [{
   title: 'example1',
   categorySlug: 'cat-1',
   categoryName: 'hello1'
}
{
   title: 'example2',
   categorySlug: 'cat-2',
   categoryName: 'hello2'
}]

// map over the objects within array
const newArray = array.map((category) => ({ categorySlug: category.categorySlug,
   categoryName: category.categoryName, data: [category] }))

Bare in mind the map method does not mutate the original array. Meaning 'array' will stay the same but 'newArray' will contain your new array with your new data structure

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

2 Comments

categorySlug is being duplicate when more than one category slug ..can you please help
please see the question again.
0

var objectArray = [{
   title: 'example1',
   categorySlug: 'cat-1',
   categoryName: 'hello1'
},
{
   title: 'example2',
   categorySlug: 'cat-2',
   categoryName: 'hello2'
}]

var newArray = [];

for (let i = 0; i < objectArray.length; i++) {
   const items = objectArray[i];
    newArray.push({
      categorySlug: items.categorySlug,
      categoryName: items.categoryName,
       data: [items]
    })

}

console.log(newArray)

1 Comment

it is not filtering when more that one categorySlug
0

What you want to achieve is more like grouping and not filtering. I think the below script will satisfy you.

const array = [{
    title: 'example1',
    categorySlug: 'cat-1',
    categoryName: 'hello1'
},
{
    title: 'example2',
    categorySlug: 'cat-2',
    categoryName: 'hello2'
},
{
    title: 'example3',
    categorySlug: 'cat-1',
    categoryName: 'hello1'
}]

const result = array
    .filter((value, index, self) => index === self.findIndex((item) => item.categoryName === value.categoryName))
    .map(({ title, ...category }) => {
        return { ...category, data: array.filter((item) => item.categoryName === category.categoryName) };
    });

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.