如何将对象与某些值的位置不相同的对象数组进行比较

时间:2018-07-23 01:49:36

标签: javascript zapier

请注意,我仍然是Java语言的初学者,对于我在以下逻辑上的任何反馈,我们将不胜感激。

(此代码是为Zapier编写的),我正在尝试创建搜索功能以在联系人列表中查找联系人。 静态信息是移动的,名字和姓氏。然后,列表最多可以包含10个自定义字段(可以命名为用户想要的任何名称)。进行API调用以返回信息时,自定义字段名称存储在“字段”对象中,其中key / vale对为field_n =“ Name”。然后将这些值用于存储联系信息的收件人对象中。

我当时认为最简单的方法是创建一个对象,该对象将存储用户正在搜索的信息:

var dataEntered = {
    "mobile" : ,
    "firstName" : ,
    "lastName" : ,
    "custom1" : ,
    "custom2" : ,
    "custom3" : ,
    "custom4" : ,
    "custom5" : ,
    "custom6" : ,
    "custom7" : ,
    "custom8" : ,
    "custom9" : ,
    "custom10" : 
};

function checkData () {
    if (mobile !== null) dataEntered.mobile = mobile;
    if (firstName !== null) dataEntered.firstName = firstName;
    if (lastName !== null) dataEntered.lastName = lastName;
    if (custom1 !== null) dataEntered.custom1 = custom1;
    if (custom2 !== null) dataEntered.custom2 = custom2;
    if (custom3 !== null) dataEntered.custom3 = custom3;
    if (custom4 !== null) dataEntered.custom4 = custom4;
    if (custom5 !== null) dataEntered.custom5 = custom5;
    if (custom6 !== null) dataEntered.custom6 = custom6;
    if (custom7 !== null) dataEntered.custom7 = custom7;
    if (custom8 !== null) dataEntered.custom8 = custom8;
    if (custom9 !== null) dataEntered.custom9 = custom9;
    if (custom10 !== null) dataEntered.custom10 = custom10;
};

然后遍历列表并将其分配给对象数组:

var listData = [{
    "mobile" : ,
    "firstName" : ,
    "lastName" : ,
    "custom1" : ,
    "custom2" : ,
    "custom3" : ,
    "custom4" : ,
    "custom5" : ,
    "custom6" : ,
    "custom7" : ,
    "custom8" : ,
    "custom9" : ,
    "custom10" : 
}];

for (var i = 0; i < contacts.length; i++) {
    listData[i].mobile = contacts[i].msisdn;
    listData[i].firstName = contacts[i].first_name;
    listData[i].lastName = contacts[i].last_name;
    if (contacts[i][obj.fields.field_1] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_1];
    if (contacts[i][obj.fields.field_2] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_2];
    if (contacts[i][obj.fields.field_3] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_3];
    if (contacts[i][obj.fields.field_4] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_4];
    if (contacts[i][obj.fields.field_5] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_5];
    if (contacts[i][obj.fields.field_6] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_6];
    if (contacts[i][obj.fields.field_7] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_7];
    if (contacts[i][obj.fields.field_8] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_8];
    if (contacts[i][obj.fields.field_9] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_9];
    if (contacts[i][obj.fields.field_10] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_10];
}

但是,我遇到的问题是列表中的自定义数据可能与用户输入数据的顺序不匹配。

通过一些研究,似乎最简单的返回匹配的方法是两个对象是否具有要以相同顺序进行比较的键/值。

我在正确的轨道上寻求最佳方法吗?

完整代码(到目前为止)

find_list_post_search: function(bundle) {
    var obj = JSON.parse(bundle.response.content)[
    var contacts = obj.recipients;
    var mobile = bundle.search_fields.contact_number;
    var firstName = bundle.search_fields.first_name;
    var lastName = bundle.search_fields.last_name;
    var custom1 = bundle.search_fields.custom_field1;
    var custom2 = bundle.search_fields.custom_field2;
    var custom3 = bundle.search_fields.custom_field3;
    var custom4 = bundle.search_fields.custom_field4;
    var custom5 = bundle.search_fields.custom_field5;
    var custom6 = bundle.search_fields.custom_field6;
    var custom7 = bundle.search_fields.custom_field7;
    var custom8 = bundle.search_fields.custom_field8;
    var custom9 = bundle.search_fields.custom_field9;
    var custom10 = bundle.search_fields.custom_field10;

var dataEntered = {
    "mobile" : ,
    "firstName" : ,
    "lastName" : ,
    "custom1" : ,
    "custom2" : ,
    "custom3" : ,
    "custom4" : ,
    "custom5" : ,
    "custom6" : ,
    "custom7" : ,
    "custom8" : ,
    "custom9" : ,
    "custom10" : 
};

var fields = {
    "field_1" : ,
    "field_2" : ,
    "field_3" : ,
    "field_4" : ,
    "field_5" : ,
    "field_6" : ,
    "field_7" : ,
    "field_8" : ,
    "field_9" : ,
    "field_10" : 
};

var listData = [{
    "mobile" : ,
    "firstName" : ,
    "lastName" : ,
    "custom1" : ,
    "custom2" : ,
    "custom3" : ,
    "custom4" : ,
    "custom5" : ,
    "custom6" : ,
    "custom7" : ,
    "custom8" : ,
    "custom9" : ,
    "custom10" : 
}];

function checkFields () {
    if (obj.fields !== undefined) {
        if (obj.fields.field_1 !== null) fields.field_1 = obj.fields.field_1;
        if (obj.fields.field_2 !== null) fields.field_2 = obj.fields.field_2;
        if (obj.fields.field_3 !== null) fields.field_3 = obj.fields.field_3;
        if (obj.fields.field_4 !== null) fields.field_4 = obj.fields.field_4;
        if (obj.fields.field_5 !== null) fields.field_5 = obj.fields.field_5;
        if (obj.fields.field_6 !== null) fields.field_6 = obj.fields.field_6;
        if (obj.fields.field_7 !== null) fields.field_7 = obj.fields.field_7;
        if (obj.fields.field_8 !== null) fields.field_8 = obj.fields.field_8;
        if (obj.fields.field_9 !== null) fields.field_9 = obj.fields.field_9;
        if (obj.fields.field_10 !== null) fields.field_10 = obj.fields.field_10;
    }
}

function checkData () {
    if (mobile !== null) dataEntered.mobile = mobile;
    if (firstName !== null) dataEntered.firstName = firstName;
    if (lastName !== null) dataEntered.lastName = lastName;
    if (custom1 !== null) dataEntered.custom1 = custom1;
    if (custom2 !== null) dataEntered.custom2 = custom2;
    if (custom3 !== null) dataEntered.custom3 = custom3;
    if (custom4 !== null) dataEntered.custom4 = custom4;
    if (custom5 !== null) dataEntered.custom5 = custom5;
    if (custom6 !== null) dataEntered.custom6 = custom6;
    if (custom7 !== null) dataEntered.custom7 = custom7;
    if (custom8 !== null) dataEntered.custom8 = custom8;
    if (custom9 !== null) dataEntered.custom9 = custom9;
    if (custom10 !== null) dataEntered.custom10 = custom10;
};

checkFields();
checkData();

for (var i = 0; i < contacts.length; i++) {
    listData[i].mobile = contacts[i].msisdn;
    listData[i].firstName = contacts[i].first_name;
    listData[i].lastName = contacts[i].last_name;
    if (contacts[i][obj.fields.field_1] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_1];
    if (contacts[i][obj.fields.field_2] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_2];
    if (contacts[i][obj.fields.field_3] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_3];
    if (contacts[i][obj.fields.field_4] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_4];
    if (contacts[i][obj.fields.field_5] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_5];
    if (contacts[i][obj.fields.field_6] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_6];
    if (contacts[i][obj.fields.field_7] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_7];
    if (contacts[i][obj.fields.field_8] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_8];
    if (contacts[i][obj.fields.field_9] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_9];
    if (contacts[i][obj.fields.field_10] !== undefined) listData[i].custom1 = contacts[i][obj.fields.field_10];
}

1 个答案:

答案 0 :(得分:0)

是的,但是您实际上应该使用map数据结构来执行此操作。那么您只需遍历各个字段并进行设置即可(有关如何初始化地图和向地图添加数据的示例,请参见链接。)

var map = new Map();

for(var i < 0; i < map.length; i++)
{
   map["custom" + "i"] = bundle.search_fields["custom_field" + i.toString()];
}
//you can add the other fields here too.
相关问题