简体   繁体   English

在具有 id 的数组与具有对象和相同 id 的数组之间查找匹配的 uniqueId

[英]Find matching uniqueId between array with id's and array with objects and same id's

I have the following object and array that have matching Id's but because of a drag and drop feature a simple map without a find doesn't work, because they are not matching in order.我有以下 object 和具有匹配 ID 的数组,但由于拖放功能,没有查找的简单 map 不起作用,因为它们不按顺序匹配。 I will explain.我会解释。

I have this object (which is my updated values after a form submit)我有这个 object(这是我提交表单后的更新值)

data variable in my code below我下面代码中的data变量

{"we98Yt8JLyxkcJorf":"Kanye","yG6JiFaBBut6XgHpj":"2813348004","RcpDcF72K2aN9fub4":"kanye@usa.gov","ShBTee9MNxvRNG3XN":"123 White House Avenue","GSCZMMwscKh3ZaKix":"473 estate drive","Jmr7HoYmfXJWHMwmr":"2813348004"}

and I have this array with objects in them that correspond to the ids我有这个数组,其中包含与 ids 对应的对象

cards variable in my code below我下面的代码中的cards变量

[{"inputType":"shortText","uniId":"we98Yt8JLyxkcJorf","label":"First Name:","value":"Kanye"},{"inputType":"phoneNumber","uniId":"yG6JiFaBBut6XgHpj","label":"Cell Phone Number","value":"2813348004"},{"inputType":"email","uniId":"RcpDcF72K2aN9fub4","label":"Work Email","value":"kanye@usa.gov"},{"inputType":"multipleChoice","uniId":"GSCZMMwscKh3ZaKix","label":"Preferred Method of Contact","value":"2813348004","options":[{"uniId":"poXf65zi8NDko5Je4","label":"Email"},{"uniId":"FmvYT4cbY8JotAaqA","label":"Cell Phone"}]},{"inputType":"Address","uniId":"ShBTee9MNxvRNG3XN","label":"Home Address","value":"123 White House Avenue"},{"inputType":"dropDown","uniId":"Jmr7HoYmfXJWHMwmr","label":"How did you find us?","value":"2813348004","options":[{"uniId":"EQj9MXdnaBjmCkAKZ","label":"Google"},{"uniId":"EbbhhqSd4K6sAsQ3T","label":"Referral"}]}]

Now I want to update the array with the new data, there is a uniqueId field on both of them that match, but I keep failing at trying to match them and update the array so I can submit that to the db in the format it needs ot be which is it's current state.现在我想用新数据更新数组,它们都有一个匹配的 uniqueId 字段,但我一直未能尝试匹配它们并更新数组,因此我可以以所需的格式将其提交给数据库不是当前的 state。

This is what I have tried thus far it returns undefined这是我迄今为止尝试过的,它返回 undefined

   const test = Object.keys(data);

    console.log(test);

    let testArray = [];

    test.map((item) => {
      let testArraySingle = cards.find((x) => {
        x.uniId === item;
      }).value;

      testArray.push(testArraySingle);
    });

    console.log(testArray);

I have tried this also but it returns incorrect because it only matches it loops how many times of the array.length but doesnt keep checking, if that makes sense我也试过这个但它返回不正确,因为它只匹配它循环了多少次 array.length 但不继续检查,如果这有意义

const dataArray = Object.entries(data);

    let newArray = [];

    cards.map((card, i) => {
      var checkId = dataArray[i][0];

      let matchesArray = [];

      if (card.uniId === checkId) {
        const new_obj = { ...cards[i], value: dataArray[i][1] };
        newArray.push(new_obj);
      }
      // }
    });

Hope somebody can help with this.希望有人可以帮助解决这个问题。 Thanks!谢谢!

You could use combination of Array.prototype.filter() and Array.prototype.map() method to get your result.您可以结合使用Array.prototype.filter()Array.prototype.map()方法来获得结果。 Traverse data keys using map and filter uniId by filter method and again use map to get the all the values.使用 map 遍历数据键并通过 filter 方法过滤 uniId 并再次使用 map 获取所有值。

 const data = { we98Yt8JLyxkcJorf: 'Kanye', yG6JiFaBBut6XgHpj: '2813348004', RcpDcF72K2aN9fub4: 'kanye@usa.gov', ShBTee9MNxvRNG3XN: '123 White House Avenue', GSCZMMwscKh3ZaKix: '473 estate drive', Jmr7HoYmfXJWHMwmr: '2813348004', }; const cards = [ { inputType: 'shortText', uniId: 'we98Yt8JLyxkcJorf', label: 'First Name:', value: 'Kanye', }, { inputType: 'phoneNumber', uniId: 'yG6JiFaBBut6XgHpj', label: 'Cell Phone Number', value: '2813348004', }, { inputType: 'email', uniId: 'RcpDcF72K2aN9fub4', label: 'Work Email', value: 'kanye@usa.gov', }, { inputType: 'multipleChoice', uniId: 'GSCZMMwscKh3ZaKix', label: 'Preferred Method of Contact', value: '2813348004', options: [ { uniId: 'poXf65zi8NDko5Je4', label: 'Email' }, { uniId: 'FmvYT4cbY8JotAaqA', label: 'Cell Phone' }, ], }, { inputType: 'Address', uniId: 'ShBTee9MNxvRNG3XN', label: 'Home Address', value: '123 White House Avenue', }, { inputType: 'dropDown', uniId: 'Jmr7HoYmfXJWHMwmr', label: 'How did you find us?', value: '2813348004', options: [ { uniId: 'EQj9MXdnaBjmCkAKZ', label: 'Google' }, { uniId: 'EbbhhqSd4K6sAsQ3T', label: 'Referral' }, ], }, ]; const ret = Object.keys(data).map((x) => { const tmp = cards.filter((y) => y.uniId === x).map((z) => { const obj = {...z }; obj.value = data[x]; return obj; }); return tmp; }).flat(); console.log(ret);

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

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