根据 id 和 return 数组 A 和数组 B 中的一列从两个数组中查找同步用户

Find synced users from two arrays based on id and return array A with one column from array B

我正在尝试在两个数组(objArray1 和 objArray2)和 return objArray1 中的数据以及 Objarray2 中的 'aid' 之间找到同步用户列表。我有下面的代码工作,但我想看看是否可以 return 'aid' 从 objArray2 以及下面的格式。

下面的代码示例

// SystemA
    const objArray1 = [
      { id: "1", name: "John" },
      { id: "2", name: "Jack" },
      { id: "3", name: "Sam" },
      { id: "4", name: "Bill" },
    ];

    // SystemB
    const objArray2 = [
      { id: "1", name: "John", aid:"uuud2905555" },
      { id: "3", name: "Sam" }, aid:"uuud29029303"
      { id: "5", name: "Bob" }, aid:"uuud29435454"
    ];

    const array1IDs = objArray1.map((item) => {
      return item.id
    })
    // Result: array1IDs = ["1", "2", "3", "4"];

    const array2IDs = objArray2.map((item) => {
      return item.id
    })
    // Result: array2IDs = ["1", "3", "5"];

    // FIND SYNCED USERS
    // Compare the id value of each item in objArray1 with each item of objArray2
    // Return the ones that match. 
    const syncedUsers = objArray1.filter((item) => {
      const found = objArray2.find((element) => element.id === item.id);
      return found;
    });

必需 JSON 格式,请注意所有匹配项都应 return 来自 objArray1,但 'aid' 来自 objArray2

{
            "records": [
                {
                    "id": {aid},                // from objArray2
                    "fields": {
                        "Name":{name},          // from objArray1  
                        "sid": {id}             // from objArray1
                }
                ]
    }

下面介绍的是实现所需 objective.

的一种可能方法

代码段

// method to create result as "JSON"
const createJSON = (arr1, arr2) => (
  // use "stringify" to transform JavaScript object into JSON
  JSON.stringify({
    // set-up the "records" prop
    records: arr2
      .filter(          // filter to keep only those that have matching "id"
        ({ id }) => arr1.some(ob => ob.id === id)
      )
      .map(             // de-structure & rename props to match desired objective
        ({ id : sid, name : Name, aid: id }) => ({
          id,
          fields: {Name, sid}
        })
      )
  })
);

// SystemA
const objArray1 = [
  { id: "1", name: "John" },
  { id: "2", name: "Jack" },
  { id: "3", name: "Sam" },
  { id: "4", name: "Bill" },
];

// SystemB
const objArray2 = [
  { id: "1", name: "John", aid:"uuud2905555" },
  { id: "3", name: "Sam", aid:"uuud29029303" },
  { id: "5", name: "Bob", aid:"uuud29435454" },
];

console.log('result as a JSON: ', createJSON(objArray1, objArray2));
.as-console-wrapper { max-height: 100% !important; top: 0 }

说明

在上面的代码段中添加了内联评论。


编辑 使用 array-1 中的 nameid。使用 restOfArr1Props 来说明要包含的 array-1 对象的所有其他属性。

const createJSON = (arr1, arr2) => (
  JSON.stringify({
    records: arr1
      .filter(
        ({ id }) => arr2.some(ob => ob.id === id)
      )
      .map(
        ({ id : sid, name : Name, ...restOfArr1Props }) => ({
          id: arr2.find(a2 => a2.id === sid)?.aid ?? 'missing aid',
          fields: {
            Name, sid, ...restOfArr1Props
          },
        })
      )
  })
);

// SystemA
const objArray1 = [
  { id: "1", name: "John", prop1: 'value11', prop2: 'value12' },
  { id: "2", name: "Jack", prop1: 'value21', prop2: 'value22' },
  { id: "3", name: "Sam", prop1: 'value31', prop2: 'value32' },
  { id: "4", name: "Bill", prop1: 'value41', prop2: 'value42' },
];

// SystemB
const objArray2 = [
  { id: "1", name: "John", aid:"uuud2905555" },
  { id: "3", name: "Sam", aid:"uuud29029303" },
  { id: "5", name: "Bob", aid:"uuud29435454" },
];

console.log('result as a JSON: ', createJSON(objArray1, objArray2));
.as-console-wrapper { max-height: 100% !important; top: 0 }

const objArray1 = [
 { id: '1', name: 'John', type: 'bully' },
 { id: '2', name: 'Jack', type: 'sporty' },
 { id: '3', name: 'Sam', type: 'kind' },
 { id: '4', name: 'Bill', type: 'poliet' },
];


const objArray2 = [
{ id: '1', name: 'John', aid: 'uuud2905555' },
{ id: '3', name: 'Sam', aid: 'uuud29029303' },
{ id: '5', name: 'Bob', aid: 'uuud29435454' },
];

const syncedUsers = { records: [] };

 for (let user1 of objArray1) {
  const foundUser = objArray2.find(user2 => user2.id === user1.id);

   if (foundUser) {
    syncedUsers.records.push({
    id: foundUser.aid,
    fields: { sid: user1.id, name: user1.name, ...user1 },
      });
    }
  }

console.log(JSON.stringify(syncedUsers));