如果数组字符串与对象属性值匹配,则JavaScript返回其他对象

dba5bblo  于 2023-04-04  发布在  Java
关注(0)|答案(3)|浏览(126)

我有一个简单的字符串id和对象数组,在初始加载时,我将这个IdsMap到这个对象,并将checked属性设置为true

const Ids = ['743156', '743157']
[
    {
        "id": "743156",
        "role": "Authorized Redistributor (AR)",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "743157",
        "role": "System Of Record (SOR)",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "743158",
        "role": "Authorized Redistributor (AR)",
        "checked": false,
        "checkBoxPatched": true
    },
    {
        "id": "743159",
        "role": "System Of Record (SOR)",
        "checked": false,
        "checkBoxPatched": true
    },
    {
        "id": "743976",
        "role": "Authorized Redistributor (AR)",
        "checked": false,
        "checkBoxPatched": true
    },
]

现在,用户可以选择通过选中复选框使checked为true来更新,这可能会使其他Ids checked属性为true。我如何才能获得checked属性为true的对象,但它不应该是两个checked为true的对象的初始值。因此,例如,我取消勾选ID为743156的复选框,并选中743158,我应该得到下面的两个对象,如果用户没有选中任何新的复选框,它应该返回空对象{}而不是初始选中的复选框,id为743156 & 743157,checked为true

{
        "id": "743157",
        "role": "System Of Record (SOR)",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "743158",
        "role": "Authorized Redistributor (AR)",
        "checked": true,
        "checkBoxPatched": true
    }
vnjpjtjt

vnjpjtjt1#

这里的same_ids_lists函数用来检查被检查的项目是否有任何变化。如果列表数组总是以相同的顺序排列,则可能没有必要对它们进行排序和复制。

const ids = ['743156', '743157'];
const data = [
    {
        "id": "743156",
        "role": "Authorized Redistributor (AR)",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "743157",
        "role": "System Of Record (SOR)",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "743158",
        "role": "Authorized Redistributor (AR)",
        "checked": false,
        "checkBoxPatched": true
    },
    {
        "id": "743159",
        "role": "System Of Record (SOR)",
        "checked": false,
        "checkBoxPatched": true
    },
    {
        "id": "743976",
        "role": "Authorized Redistributor (AR)",
        "checked": false,
        "checkBoxPatched": true
    },
];

const same_ids_lists = (list_1, list_2) =>
  [...list_1].sort().join('-') === [...list_2].sort().join('-');

const get_new_checked_if_changed = (data, last_ids) => {
  const checked = data.filter( (item) => item.checked );
  
  const is_unchanged = same_ids_lists(
    checked.map( (item) => item.id ),
    last_ids
  );
  
  return is_unchanged ? false : checked;
};

console.log('When unchanged: ', get_new_checked_if_changed(data, ids));

data[0].checked = false;
data[2].checked = true;

console.log('When changed: ', get_new_checked_if_changed(data, ids));

编辑:将get_new_checked_if_changed更改为在未更改时返回false,否则将无法区分未更改和未选中的项。

bsxbgnwa

bsxbgnwa2#

要省略前两个分别具有ids数组id的对象并获取检查值

const data = [
    {
        "id": "743156",
        "role": "Authorized Redistributor (AR)",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "743157",
        "role": "System Of Record (SOR)",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "743158",
        "role": "Authorized Redistributor (AR)",
        "checked": false,
        "checkBoxPatched": true
    },
    {
        "id": "743159",
        "role": "System Of Record (SOR)",
        "checked": false,
        "checkBoxPatched": true
    },
    {
        "id": "743976",
        "role": "Authorized Redistributor (AR)",
        "checked": false,
        "checkBoxPatched": true
    }
];

const ids = ["743156", "743157"];
const checkedArr = [];
data.map((d) => {
    if (!ids.includes(d.id) && d.checked === true) {
        checkedArr.push(d);
    }
})

console.log(checkedArr);
bmp9r5qi

bmp9r5qi3#

你可以简单地在JS中使用**filter()**方法

const arrObj = [
    {
        "id": "743156",
        "role": "Authorized Redistributor (AR)",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "743157",
        "role": "System Of Record (SOR)",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "743158",
        "role": "Authorized Redistributor (AR)",
        "checked": false,
        "checkBoxPatched": true
    },
    {
        "id": "743159",
        "role": "System Of Record (SOR)",
        "checked": false,
        "checkBoxPatched": true
    },
    {
        "id": "743976",
        "role": "Authorized Redistributor (AR)",
        "checked": false,
        "checkBoxPatched": true
    },
]

const filterdData = arrObj.filter((_obj)=> _obj.checked === true);

console.log(filterdData)

Array.prototype.filter()

相关问题