简体   繁体   English

如何比较javascript中两个不同arrays的对象

[英]How to compare objects of two different arrays in javascript

I have a react application and in that I have the following list of objects.我有一个 React 应用程序,其中有以下对象列表。

const orderItems = [
  {
    id: 1,
    name: "item 1",
    price: "100",
    availableQty: 2,
    out: ""
  },
  {
    id: 2,
    name: "item 2",
    price: "100",
    availableQty: 2,
    out: ""
  },
  {
    id: 3,
    name: "item 3",
    price: "100",
    availableQty: 2,
    out: ""
  },
  {
    id: 4,
    name: "item 4",
    price: "100",
    availableQty: 2,
    out: ""
  }
];

Im mapping through the orderItems list and display the name with a checkbox .我通过orderItems列表进行映射并显示带有checkbox的名称。 I have a state named selectedItem and when the checkbox is checked, new object adds to the selectedItem along with the id of the orderItems as shown in the following code.我有一个名为selectedItem的state,当检查复选框时,新的orderItems将添加到selectedItem以及订单列的id以及以下代码中所示。

const handleSelect = (e, item) => {
    const { checked } = e.target;

    const newObj = {
      id: item.id,
      isAdded: true,
      reason: "",
      issue: "",
      availableStock: ""
    };

    if (checked) {
      setselectedItem((previousState) => [...previousState, newObj]);
    }
    if (checked === false) {
      const filteredItems = selectedItem.filter(
        (singleItem) => singleItem.id !== item.id
      );

      setselectedItem(filteredItems);
    }
  };

In the react app, Im mapping the orderItems and showing the name with a checkbox.在 React 应用程序中,我映射orderItems并使用复选框显示name When the checkbox is checked, I add a new object to selectedItem with the id of orderItems and if the selectedItem is empty, I display a as "list is empty".选中复选框后,我将新的 object 添加到idorderItemsselectedItem ,如果selectedItem为空,我将显示为“列表为空”。 If checkbox is selected, I display a input tag under the name and if its not selected, i display a label with the text "not selected" as shown in the code below.如果复选框被选中,我会在name下方显示一个input标签,如果未选中,我会显示一个label和文本“未选中”,如下面的代码所示。

<div className="app">
      {items.map((item) => (
        <div className="issue">
          <input type="checkbox" onChange={(e) => handleSelect(e, item)} />
          <label>{item.name}</label>
          {selectedItem.length > 0 ? (
            <>
              {selectedItem.map((singleItem) =>
                singleItem.id === item.id ? (
                  <input type="number" />
                ) : (
                  <label>not selected</label>
                )
              )}
            </>
          ) : (
            "list is empty"
          )}
        </div>
      ))}
    </div>

The issue here is, when one item is checked , it show input tag under selected item and shows "not selected" label under other items but if i select two checkboxes, both not selected label and input tag displays.这里的问题是,当checked一个项目时,它显示了选定项目下的input标签,并在其他项目下显示“未选择” label,但是如果I select两个复选框,则未选择label和输入标签显示。 If i select 3 checkboxes, not selected label displays two times with input tag.如果 i select 3 个复选框,未选中 label 显示两次带有输入标签。

What i want is to display only the input tags under selected items and display not selected label under unchecked items.我想要的是只在选中的项目下显示输入标签,在未选中的项目下显示未选中的 label。

codesandbox link: https://codesandbox.io/s/minsaf-2jjt2g codesandbox链接: https://codesandbox.io/s/minsaf-2jjt2g

You are mapping the selectedItem array for every element in the item array.您正在为item数组中的每个元素映射selectedItem数组。 This means for any element in the array that some condition matches and both the input and label are rendered.这意味着对于某些条件匹配的数组中的任何元素,输入和 label 都会被渲染。

If I understand the code and your expected behavior I think you should conditionally render a single input or label based on if the selectedItem array includes an element matching by id.如果我理解代码和您的预期行为,我认为您应该根据selectedItem数组是否包含按 id 匹配的元素有条件地呈现单个输入或 label。

Example:例子:

<div className="app">
  {items.map((item) => (
    <div className="issue">
      <input type="checkbox" onChange={(e) => handleSelect(e, item)} />
      <label>{item.name}</label>
      {selectedItem.length ? (
        selectedItem.some((singleItem) => singleItem.id === item.id) ? (
          <input type="number" />
        ) : (
          <label>not selected</label>
        )
      ) : (
        "list is empty"
      )}
    </div>
  ))}
</div>

编辑如何在 JavaScript 中比较两个不同数组的对象

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

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