0

I'm facing an issue to fix a bug.

I'm getting data from dataBase. the data ( object ) contains an array of names. I have useState hook that contains an array of objects. The value is names. I want to delete every object from the useState hook that much the name from the array fetched for dataBase.

Exp:

response.data.players = ["player1", "player2"]

const [names, setNames] = useState([
{value: "player1", label: "player1"},
{value: "player3", label: "player3"},
{value: "player2", label: "player2"},
{value: "player5", label: "player5"},
])

I want the names become like this:

[
{value: "player3", label: "player3"},
{value: "player5", label: "player5"},
]

with minimum time complexity ( I'm already using .map to serve other hook with the names, if I can use it it will be perfect for the time complexity )

Thank you

2
  • a bug or code optimization with min time complexity? what are the contents of .map? Commented Sep 15, 2021 at 2:10
  • response.data.condidates.map((elem, index) => { dataCondidate.push(elem) }) this one is serving another functionality Commented Sep 15, 2021 at 2:12

2 Answers 2

1

That just sounds like a filter to me. Can't you just do something like this:

setNames(names.filter(name => !response.data.players.includes(name.label)))

For reference, here's that working without React:

const names = [{
    value: "player1",
    label: "player1"
  },
  {
    value: "player3",
    label: "player3"
  },
  {
    value: "player2",
    label: "player2"
  },
  {
    value: "player5",
    label: "player5"
  },
];

const response = {
  data: {
    players: ["player1", "player2"],
  },
};


const filteredNames = names.filter(name => !response.data.players.includes(name.label))

console.log(filteredNames);

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

1 Comment

It is always a great option to make the code runnable.
1

You can efficiently achieve the result using Set and filter

const names = [
  { value: "player1", label: "player1" },
  { value: "player3", label: "player3" },
  { value: "player2", label: "player2" },
  { value: "player5", label: "player5" },
];

const players = ["player1", "player2"];

const set = new Set(players);
const result = names.filter(({ label }) => !set.has(label));
console.log(result);

2 Comments

I didn't use set before, it's new for me. Ill read about it. Also i tried the code and it worked. I really appreciate your help:)
It's added in ES6 and If you have a very large dataset in players then It is better to use the has method of Set to check for existence rather than includes of an array. has is optimized for checking existence. If you have a small data set then you can use either one. Happy learning 😊

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.