1

I'm adding several id's to an array when a checkbox is checked.

<input type="checkbox" name="vehicle2" onClick={() => this.addEvcGroupToArray(item.stations.map((item, key) => { return (item.stationID) }))}

This is the function that adds the ids to the array

     addEvcGroupToArray(id) {
        console.log("group_id ",id);
        var stationId = {};
        id.map((item, key) => {
            stationId = {
                stationId: item
            }
        })
        var evcGroupIdArray = this.state.stationIdArray.concat(stationId);
        this.setState({ stationIdArray: evcGroupIdArray })
    }

For example console.log("group_id ",id); outputs an array of ids like ["8"] (Here it's just one id,but this can be any number of ids). I want to prevent adding the same id's if the user click on the checkbox again.

I tried something like this using Set. But it did not work. How can i do this?

  addEvcGroupToArray(id) {
    console.log("group_id ",id);
    var stationId = {};
    id.map((item, key) => {
        stationId = {
            stationId: item
        }
    })
    var evcGroupIdArray = this.state.stationIdArray.concat(stationId);
    var uniqueArray = [...new Set(evcGroupIdArray)];
    this.setState({ stationIdArray: uniqueArray })
}
1
  • I am not familiar with React, however,why do you use map without assiging id.map? Commented Dec 23, 2019 at 13:01

5 Answers 5

1

First initialize an array instead of an object like

let stationId = [];

After try adding this instead of adding id.map

id.forEach(item => {
  if (!this.state.stationIdArray.some(item => item === id)) {
   stationId.push(item);
 } 
})

and set the state

this.setState({ stationIdArray: [...stationIdArray, stationId] });

I hope it helps.

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

Comments

1

map already returns an array, it is not recommended to use it to loop through values although you can do it.

const myArr = [
  {stationId: 3},
  {stationId: 1},
  {stationId: 2},
  {stationId: 3},
  {stationId: 3},
];


const result = [];
const map = new Map();
for (const item of myArr) {
if(!map.has(item.stationId)){
    map.set(item.stationId, true);    // set any value to Map
    result.push({
        stationId: item.stationId
    });
}
}
console.log(result)

Comments

0

Try cleaning id array just before going into the rest of the logic

addEvcGroupToArray (id) {
  console.log("group_id ", id);
  const clean = new Set(id); // <- remove duplicated ids
  var stationId = {};
  clean.map((item, key) => {
    stationId = {
      stationId: item
    };
  });
  var evcGroupIdArray = this.state.stationIdArray.concat(stationId);
  this.setState({ stationIdArray: evcGroupIdArray });
};

Comments

0

lodash _.uniqueBy can be applied on evcGroupIdArray.

_.uniqBy(evcGroupIdArray, 'stationId');

Comments

0

How about this?

function addEvcGroupToArray(id) {
    console.log("group_id ",id);
    // Get the current state id array
    const { stationIdArray } = this.state;
    // Append new id array
    const uniqueArray = [...new Set([...stationIdArray, ...id])];
    this.setState({ stationIdArray: uniqueArray })
}

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.