Home > other >  Filter array object by name
Filter array object by name

Time:07-06

I have array object like this, can you help me?

[{id: 1, name: "contractor"},
{id: 2, name: "owner", },
{id: 3, name: "manager", },
{id: 4, name: "customer", },
{id: 5, name: "admin",}]

I want a format like that:

if admin

[admin: 
{id: 1, name: "contractor"},
{id: 2, name: "owner", },
{id: 3, name: "manager", },
{id: 4, name: "customer", }]

if contractor

[contractor:
{id: 2, name: "owner", },
{id: 3, name: "manager", },
{id: 4, name: "customer", }]

if owner

[owner:
{id: 3, name: "manager", },
{id: 4, name: "customer", }]

if manager

[manager:
{id: 4, name: "customer", }]

CodePudding user response:

Based on the comments, i guess you need this

let array = [
  {id: 1, name: "contractor"},
  {id: 2, name: "owner"},
  {id: 3, name: "manager"},
  {id: 4, name: "customer"},
  {id: 5, name: "admin"}
]

const admin = { admin: array.filter(obj => obj.id < 5) }
const contractor = { contractor: array.filter(obj => [2, 3, 4].includes(obj.id)) }
const owner = { owner: array.filter(obj => [3, 4].includes(obj.id)) }
const manager = { manager: array.filter(obj => obj.id === 4) }

console.log(admin, contractor, owner, manager)

CodePudding user response:

IF you want to make it so it will only generate the array if it is included in the name, then you can use array.reduce

const array = [
  {id: 1, name: "contractor"},
  {id: 2, name: "owner"},
  {id: 3, name: "manager"},
  {id: 4, name: "customer"},
  {id: 5, name: "admin"}
];

const objects = array.reduce((prev, curr) => {
  if (prev[curr.name]) {
return {
  ...prev,
  [prev[curr.name]]: prev[curr.name].concat(curr),
}
  }

  return {
    ...prev,
    [curr.name]: [curr]
  }
}, {});

console.log(objects);

CodePudding user response:

You have to sort the data first, as you can see the below code. I hope this would be helpful. thanks

const array = [
    { id: 1, name: "contractor", sort: 2 },
    { id: 2, name: "owner", sort: 3 },
    { id: 3, name: "manager", sort: 4 },
    { id: 4, name: "customer", sort: 5 },
    { id: 5, name: "admin", sort: 1 }
];
let nameArr = [];
let arr = [];
let newArray = [];
const sortedData = array.sort((a, b) => a.sort - b.sort);

sortedData.forEach(ele => {
    array.forEach(item => {
        if (item.name !== ele.name) {
            if (newArray.length) {
                if (!nameArr.includes(item.name) && (item.name !== ele.name)) {
                    arr.push(item);
                }
            } else {
                arr.push(item);
            }
        }
    })
    newArray.push({ [ele.name]: arr });
    nameArr.push(ele.name);
    arr = []
})
console.log(newArray)

Result will be this:

[
    {
        "admin": [
            {
                "id": 1,
                "name": "contractor",
                "sort": 2
            },
            {
                "id": 2,
                "name": "owner",
                "sort": 3
            },
            {
                "id": 3,
                "name": "manager",
                "sort": 4
            },
            {
                "id": 4,
                "name": "customer",
                "sort": 5
            }
        ]
    },
    {
        "contractor": [
            {
                "id": 2,
                "name": "owner",
                "sort": 3
            },
            {
                "id": 3,
                "name": "manager",
                "sort": 4
            },
            {
                "id": 4,
                "name": "customer",
                "sort": 5
            }
        ]
    },
    {
        "owner": [
            {
                "id": 3,
                "name": "manager",
                "sort": 4
            },
            {
                "id": 4,
                "name": "customer",
                "sort": 5
            }
        ]
    },
    {
        "manager": [
            {
                "id": 4,
                "name": "customer",
                "sort": 5
            }
        ]
    },
    {
        "customer": []
    }
]
  • Related