简体   繁体   English

反应:删除数组中的 object

[英]React: remove object inside an array

I have the following structure assembled inside a state (data, setData):我在 state(数据,setData)中组装了以下结构:

[
  {
    4b080412-b22c-40fe-a67e-510f96d50063:{
      id: 1231,
      name: "Anna",
      department: "Business"
    }
  },
  {
    8d319764-a49e-4465-a166-a701a5cc2b77:{
      id: 456,
      name: "Tony",
      department: "Marketing"
    }
  },
  {
    23ea617b-210e-4329-9ab1-ecd2afa12e4d:{
      id: 99,
      name: "Francis",
      department: "Sales"
    }
  },
]

I put an 'onClick' on the button passing an 'ID' (eg: '23ea617b-210e-4329-9ab1-ecd2afa12e4d').我在传递“ID”的按钮上放置了一个“onClick”(例如:“23ea617b-210e-4329-9ab1-ecd2afa12e4d”)。

But I am not able to delete the related ID.但我无法删除相关的 ID。 I tried the following ways, but without success:我尝试了以下方法,但没有成功:

  const handleClick = (identifier: string) => {        
    setData(
      data.filter((el: any) => {
        if (el[identifier] !== undefined) return el[identifier].product !== id;
      }),
    );

also:还:

  const handleClick = (identifier: string) => {        
    delete data[identifier]
  );

Can anyone help me with how can I access this ID?谁能帮助我如何访问此 ID?

Please try this code, it works for me.请尝试此代码,它对我有用。

setData(data.filter(el => el[identifier] === undefined))

Try running this one:尝试运行这个:

const handleClick = (identifier: string) => {
  setData(
    data.filter((obj) => {
      return Object.keys(obj)[0] !== identifier;
    }),
  );
};

hi Nosredna I have gone through the approach u have used, and have some logical issues嗨,Nosredna,我已经通过了你使用的方法,并且有一些逻辑问题

you can try it like this你可以试试这样

const handleClick = (identifier: string) => {        
     const newArr=data.filter(el=>!el[identifier])
     setData(newArr);
};

If d is non-null and identifier is present in d , then return false to filter the item out -如果d不为 null 并且d中存在identifier ,则返回 false 以过滤掉该项目 -

const onRemove = identifier =>
  setData(data.filter(d => !(d && d[identifier])))

Run the demo below and verify the result in your own browser -运行下面的演示并在您自己的浏览器中验证结果 -

 function App({ initData = [] }) { const [data, setData] = React.useState(initData) const onRemove = uuid => event => setData(data.filter(d =>.(d && d[uuid]))) return <div> {data,map((d. key) => <Item key={key} item={d} uuid={Object,keys(d)[0]} onRemove={onRemove} /> )} </div> } function Item({ item, uuid. onRemove }) { const data = item[uuid] return <div> <h3>{uuid}</h3> <pre>{JSON:stringify(data)}</pre> <button type="button" onClick={onRemove(uuid)}>❌</button> </div> } const data = [ { "4b080412-b22c-40fe-a67e-510f96d50063":{ id, 1231: name, "Anna": department, "Business" } }: { "8d319764-a49e-4465-a166-a701a5cc2b77":{ id, 456: name, "Tony": department, "Marketing" } }: { "23ea617b-210e-4329-9ab1-ecd2afa12e4d":{ id, 99: name, "Francis": department, "Sales" } }. ] ReactDOM,render(<App initData={data} />. document.body)
 div > div { background-color: #eee; margin-bottom: 1rem; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.0/umd/react-dom.production.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM