Home > Net >  onChange CheckBox antd check id and add to new array Javascript/Reactjs
onChange CheckBox antd check id and add to new array Javascript/Reactjs

Time:10-18

I have CheckBox.Group, when checked the value return array and id exp: ["view"] 1

i have local array call finalArray now i want to check the function onChange have

same id => replace the new value in array

different id => push new object {["view"] 1} to the array

export default function App() {
  const options = [
    {
      label: "view",
      value: "view"
    },
    {
      label: "add",
      value: "add"
    },
    {
      label: "edit",
      value: "edit"
    },
    {
      label: "delete",
      value: "delete"
    }
  ];

  let finalArray = [];
  const onChange = (arr, id) => {
    console.log(arr, id);
    if (!finalArray.length) {
      // Load first time have no item => add new item to array
      finalArray.push({ arr, id });
    } else {
      finalArray.map((item, index) => {
        // load second time have item => check same id
        if (item.id === id) {
          // if the id in finalArray === id
          // => replace new value
          finalArray[index] = { arr, id };
        } else if (item.id !== id) {
          // if the id in finalArray !== id
          // => add new object to array
          finalArray.push({ arr, id });
        }
      });
    }
    console.log(finalArray);
  };



 // array is for render 4 element checkbox
  const arrCheckbox = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];

  return (
    <div className="App">
      {arrCheckbox.map((item) => {
        return (
          <Checkbox.Group
            options={options}
            style={{ padding: "20px" }}
            key={item.id}
            // value="view"
            onChange={(e) => {
              onChange(e, item.id);
            }}
          ></Checkbox.Group>
        );
      })}
    </div>
  );
}

CodeSanBox example : https://codesandbox.io/s/mystifying-browser-b0fn0k?file=/src/App.js:0-1504

the issue is first line of checkbox i check result is right but the second line when i check it will multiple finalArray , you can see result in codesanbox

CodePudding user response:

You have used the wrong condition for different id => push new object {["view"] 1} to the array It should be outside the array iterate not inside. Because in array 1 element matches but others do not so it will add again.

import { Checkbox } from "antd";

export default function App() {
  const options = [
    {
      label: "view",
      value: "view"
    },
    {
      label: "add",
      value: "add"
    },
    {
      label: "edit",
      value: "edit"
    },
    {
      label: "delete",
      value: "delete"
    }
  ];

  let finalArray = [];
  const onChange = (arr, id) => {
    console.log(arr, id);
    if (!finalArray.length) {
      // Load first time have no item => add new item to array
      finalArray.push({ arr, id });
    } else {
      let exists = false;
      finalArray.map((item, index) => {
        // load second time have item => check same id
        if (item.id === id) {
          // if the id in finalArray === id
          // => replace new value
          if (arr.length !== 0) {
            finalArray[index] = { arr, id };
          } else {
            finalArray.splice(index, 1);
          }
          exists = true;
        }
      });
      if (!exists) {
        finalArray.push({ arr, id });
      }
    }
    console.log(finalArray);
  };

  // array is for render 4 element checkbox
  const arrCheckbox = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];

  return (
    <div className="App">
      {arrCheckbox.map((item) => {
        return (
          <Checkbox.Group
            options={options}
            style={{ padding: "20px" }}
            key={item.id}
            // value="view"
            onChange={(e) => {
              onChange(e, item.id);
            }}
          ></Checkbox.Group>
        );
      })}
    </div>
  );
}

Running sample https://codesandbox.io/s/elated-jackson-qtu87z

  • Related