2

I have two arrays like so:

const data = [
    {
        createOn: "2021-02-06T18:03",
        data: [
            {id: "1612634593915", createOn: "2021-02-06T18:03:13.915"},
            {id: "1612634622462", createOn: "2021-02-06T18:03:42.462"},
            {id: "1612634584081", createOn: "2021-02-06T18:03:04.081"},
        ]
    },
    {
        createOn: "2021-02-06T18:02",
        data: [
            {id: "1612634576145", createOn: "2021-02-06T18:02:56.145"},
            {id: "1612634557111", createOn: "2021-02-06T18:02:37.110"}
        ]
    }
]
const listId = ['1612634622462', '1612634557111'];

Then I try to remove object in array by id in listId

const remove = data.map(re => {
    re.data.filter(item => {
        return !listId.includes(item.id);
    })
});

But when I log it out, I got [undefined, undefined].

I want array return like so after remove:

const data = [
    {
        createOn: "2021-02-06T18:03",
        data: [
            {id: "1612634593915", createOn: "2021-02-06T18:03:13.915"},
            {id: "1612634584081", createOn: "2021-02-06T18:03:04.081"},
        ]
    },
    {
        createOn: "2021-02-06T18:02",
        data: [
            {id: "1612634576145", createOn: "2021-02-06T18:02:56.145"}
        ]
    }
]

Can you help me. Thank you :))

1
  • 2
    Sometimes I don't get the need to post ton of answers if there are 1-2 good approaches already.. What good does it give to the community if the answers are pretty much the same?! Commented Mar 3, 2021 at 13:42

4 Answers 4

2

const data = [
    {
        createOn: "2021-02-06T18:03",
        data: [
            {id: "1612634593915", createOn: "2021-02-06T18:03:13.915"},
            {id: "1612634622462", createOn: "2021-02-06T18:03:42.462"},
            {id: "1612634584081", createOn: "2021-02-06T18:03:04.081"},
        ]
    },
    {
        createOn: "2021-02-06T18:02",
        data: [
            {id: "1612634576145", createOn: "2021-02-06T18:02:56.145"},
            {id: "1612634557111", createOn: "2021-02-06T18:02:37.110"}
            ]
        
    }
]
const listId = ['1612634622462', '1612634557111'];

const newData = data.map(item => {
  item.data = item.data.filter(item => !listId.includes(item.id))
  return item
})

console.log(newData)

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

Comments

1

You should return the result from .map():

const data = [
    {
        createOn: "2021-02-06T18:03",
        data: [
            {id: "1612634593915", createOn: "2021-02-06T18:03:13.915"},
            {id: "1612634622462", createOn: "2021-02-06T18:03:42.462"},
            {id: "1612634584081", createOn: "2021-02-06T18:03:04.081"},
        ]
    },
    {
        createOn: "2021-02-06T18:02",
        data: [
            {id: "1612634576145", createOn: "2021-02-06T18:02:56.145"},
            {id: "1612634557111", createOn: "2021-02-06T18:02:37.110"}
        ]
    }
]
const listId = ['1612634622462', '1612634557111'];

const remove = data.map(re => {
    re.data = re.data.filter(item => {
        return !listId.includes(item.id);
    });
    return re; // return here
});
console.log(remove);

Comments

0

Your initial problem is that your inner map() function doesn't doesn't return a value - for each object in the data array. That's why is returned undefined for both iterations. Also - If you filter() the array, you have to reassign it to itself to keep only filtered elements.

data.forEach(re => {
    re.data = re.data.filter(item => {
        return !listId.includes(item.id);
    })
});

1 Comment

Yes, I know one more information. I just fix my filter func to run that. thank you
0

Sure not the best solution, but I would loop through each data object and filter the data array for each listId Object. Just like this:

data.forEach(element => {
  listId.forEach(removeId => {
    element.data = element.data.filter(e.id => e.id !== removeId);
  });
});

2 Comments

Doesn't work. You need to check e.id, not e itself. as each element of data array is an object
You are absolutely correct did not see this

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.