简体   繁体   English

通过将两个数组与对象进行比较来创建一个新的布尔值数组

[英]Create a new array of boolean values by comparing two arrays with objects

I have a question about creating JavaScript arrays.我有一个关于创建 JavaScript 数组的问题。 Here's what I want to do.这就是我想做的。

I have two arrays .我有两个数组 Each array has object(s) in them.每个数组中都有对象。

The first array has exactly 5 objects, each with a key/value pair (id/name).一个数组正好有 5 个对象,每个对象都有一个键/值对(id/name)。 This array will ALWAYS have 5 objects.这个数组总是有 5 个对象。

Here's what it looks like...这是它的样子......

[
    {
        "id": 1,
        "name": "AnalyticalAdmin"
    },
    {
        "id": 2,
        "name": "Analyst"
    },
    {
        "id": 4,
        "name": "AdminReviewer"
    },
    {
        "id": 5,
        "name": "SystemAdministrator"
    },
    {
        "id": 6,
        "name": "CaseworkSTRTechLeader"
    }
]

The second array will have either ...第二个数组具有...

  1. no objects,没有对象,

  2. 4 or less objects, 4个或更少的物体,

  3. exactly 5 objects.正好 5 个对象。

In the example below, the second array only has one of the 5 objects in the first array...在下面的示例中,第二个数组只有第一个数组中的 5 个对象中的一个...

[
    {
        "id": 4,
        "name": "AdminReviewer"
    }
]

I need to create a third array that has exactly 5 boolean values that are determined by comparing the first two arrays.我需要创建第三个数组,其中正好有 5 个布尔值,这些值是通过比较前两个数组来确定的。

For example, based on the first two arrays above, the third array would be...例如,基于上面的前两个数组,第三个数组将是......

[false, false, true, false, false]

The reason the third array would look like this is because "AdminReviewer" is the third object in the first array, so the third boolean value in the third array would be true (since it's a match).第三个数组看起来像这样的原因是因为“AdminReviewer”是第一个数组中的第三个对象,所以第三个数组中的第三个布尔值将为真(因为它是匹配的)。

But because the first, second, fourth, and fifth objects in the first array do not exist in the second array, their boolean equivalent in the third array would be false.但是因为第一个数组中的第一个、第二个、第四个和第五个对象在第二个数组中不存在,所以它们在第三个数组中的布尔等效项将是错误的。

To accomplish this, I know I need to either do a compare function on the first two arrays to create the third array (of booleans) or I need to run a for loop on the first array, comparing it to the second array to create the third array (of booleans).为此,我知道我需要对前两个数组执行比较函数以创建第三个数组(布尔值),或者我需要在第一个数组上运行for 循环,将其与第二个数组进行比较以创建第三个数组(布尔值)。

Can anyone help me with this?谁能帮我这个?

This could be done as follows:这可以按如下方式完成:

const filterStrings = filterArray.map(o => JSON.stringify(o));
const result = baseArray.map(o => filterStrings.includes(JSON.stringify(o)));

Please take a look at below runnable code and see how it works.请看下面的可运行代码,看看它是如何工作的。

 const baseArray = [ { "id": 1, "name": "AnalyticalAdmin" }, { "id": 2, "name": "Analyst" }, { "id": 4, "name": "AdminReviewer" }, { "id": 5, "name": "SystemAdministrator" }, { "id": 6, "name": "CaseworkSTRTechLeader" } ]; const filterArray = [ { "id": 4, "name": "AdminReviewer" } ]; const filterStrings = filterArray.map(o => JSON.stringify(o)); const result = baseArray.map(o => filterStrings.includes(JSON.stringify(o))); console.log(result);

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

相关问题 如何根据两个现有 arrays 的值创建新的对象数组? - How to create a new array of objects based on the values of two existing arrays? 比较两个嵌套的对象arrays,并在JS中将“id”匹配值的元素排除到新数组中 - Comparing two nested arrays of objects, and exclude the elements who match values by "id" into new array in JS 比较两个对象数组,在JS中排除匹配值的元素到新数组中 - Comparing two arrays of objects, and exclude the elements who match values into new array in JS 比较两个数组和公共对象添加到新数组 - Comparing two arrays and common objects add to new array 比较两个数组的对象的属性并将其存储在新数组中 - Comparing the properties of objects of two arrays and storing it in a new array 比较两个 arrays 并将相等的值放入新数组中 - Comparing two arrays and put equal values in new array 将两个Arrays与Objects进行比较,并使用不匹配的对象创建新数组 - Compare two Arrays with Objects and create new array with unmatched objects 如何根据 object 值从两个 arrays 创建一个新的对象数组? - How to create a new array of objects from two arrays based on object values? 获取对象数组作为比较两个 arrays 的结果? - Get array of objects as result of comparing two arrays? 迭代两个对象数组并创建新的数组对象 - Iterate over two arrays of objects and create new array object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM