如何通过jQuery在另一个数组对象中检查一组对象

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (70)

我的数组,存储在名为的变量中data

var data = [
  {
     "name": "username",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  },      
  {
     "name": "password",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  },
  {
     "name": "gender",
     "value": "other",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.select",
        "choices": [
           {
              "value": "male",
              "label": "Male"
           },
           {
              "value": "female",
              "label": "Female"
           },
           {
              "value": "other",
              "label": "Other"
           }
        ]
     }
  },
  {
     "name": "country",
     "value": "3",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.select",
        "choices": [
           {
              "value": "1",
              "label": "Australia"
           },
           {
              "value": "2",
              "label": "USA"
           },
           {
              "value": "3",
              "label": "UK"
           }
        ]
     }
  },
  {
     "name": "decision",
     "value": "not sure",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.text",
        "choices": [
           {
              "value": "yes",
              "label": "Yes"
           },
           {
              "value": "no",
              "label": "No"
           },
           {
              "value": "not sure",
              "label": "I am not sure"
           }
        ]
     }
  },
  {
     "name": "hobby",
     "value": "['book', 'movie']",
     "schema": {
        "version": 1,
        "value_type": "string",
        "name": "form.text",
        "choices": [
           {
              "value": "travel",
              "label": "Traveling"
           },
           {
              "value": "book",
              "label": "Reading"
           },
           {
              "value": "movie",
              "label": "Watching Movie"
           }
        ]
     }
  },
  {
     "name": "privacy",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  },
  {
     "name": "message",
     "value": null,
     "schema": {
        "version": 1,
        "name": "form.text"
     }
  }
]

我的对象,存储在名为的变量中formObj

var formObj = {


username: {
    value: null,
    type: 'text',
    label: 'Username',
    placeholder: 'Enter username',
    position: 'left',
    block: 'firstRow',
    order: 1,
    additionalClass: 'user'
  },
  password: {
    value: null,
    type: 'password',
    label: 'Password',
    placeholder: 'enter password',
    position: 'left',
    block: 'firstRow',
    order: 2
  },
  country: {
    value: '3',
    type: 'select',
    label: 'Where are you form?',
    //defaultText: 'Choose here',
    position: 'right',
    block: 'firstRow',
    order: 2,
    option: [{
        value: '1',
        label: 'Australia'
      },
      {
        value: '2',
        label: 'USA'
      },
      {
        value: '3',
        label: 'UK'
      }
    ]
  },
  gender: {
    value: 'female',
    type: 'select',
    label: 'Gender',
    defaultText: null,
    position: 'right',
    block: 'firstRow',
    order: 1,
    option: [{
        value: 'male',
        label: 'Male'
      },
      {
        value: 'female',
        label: 'Female'
      },
      {
        value: 'other',
        label: 'Other',
        additionalAttr: 'disabled'
      }
    ]
  },
  decision: {
    value: 'not sure',
    type: 'radio',
    label: 'Have you enjoyed it?',
    position: 'left',
    block: 'secondRow',
    order: 1,
    option: [{
        value: 'yes',
        label: 'Yes',
        additionalAttr: 'disabled'
      },
      {
        value: 'no',
        label: 'No',
      },
      {
        value: 'not sure',
        label: 'I am not sure',
      }
    ]
  },
  message: {
    value: null,
    type: 'textarea',
    label: 'Message',
    placeholder: 'type your message',
    position: 'right',
    block: 'secondRow',
    order: 2,
  },
  vehicle: {
    value: 'car',
    type: 'checkbox',
    label: 'Preferred vehicles',
    position: 'left',
    block: 'secondRow',
    order: 2,
    option: [{
        value: 'car',
        label: 'I like car'
      },
      {
        value: 'bike',
        label: 'I prefer bike'
      },
      {
        value: 'boat',
        label: 'Boat is my favorit'
      }
    ]
  },
  hobby: {
    value: ['book', 'movie'],
    type: 'checkbox',
    label: 'Hobbies',
    position: 'left',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'travel',
        label: 'Traveling'
      },
      {
        value: 'book',
        label: 'Reading'
      },
      {
        value: 'movie',
        label: 'Watching movie'
      }
    ]
  },
  privacy: {
    value: null,
    type: 'checkbox',
    label: 'Privacy',
    position: 'right',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'agree',
        label: 'I agree'
      }
    ]
  },
  email: {
    value: null,
    type: 'text',
    label: 'Email',
    placeholder: 'Enter email',
    position: 'right',
    block: 'secondRow',
    order: 1,
    additionalAttr: 'disabled',
    additionalClass: 'emailbox custom'
  }
}

这样,也有8 objectsdata(阵列)和10 objectsformObj(对象)。

我的目标是相匹配的name,从dataformObj,且仅匹配对象创建新的对象。所有value新对象都应该来自data(数组)。简而言之,创建一个newObj喜欢/来自formObjnewObj将不包含不在data(数组)中的对象。

我试着在这里写这个概念,但不明白如何继续:

for (var i = 0; i < data.length; i++) {
   if (data[i].name === formObj[key]) {
      newObj = formObj;   // take only those from formObj which exist in data
      newObj[option] = data[schema][choices];  // take the option from choices
      newObj[value] = data[value];  // take the value from value
   }
}

所以,newObj将是:

var newObj = {


username: {
    value: null,
    type: 'text',
    label: 'Username',
    placeholder: 'Enter username',
    position: 'left',
    block: 'firstRow',
    order: 1,
    additionalClass: 'user'
  },
  password: {
    value: null,
    type: 'password',
    label: 'Password',
    placeholder: 'enter password',
    position: 'left',
    block: 'firstRow',
    order: 2
  },
  country: {
    value: '3',
    type: 'select',
    label: 'Where are you form?',
    //defaultText: 'Choose here',
    position: 'right',
    block: 'firstRow',
    order: 2,
    option: [{
        value: '1',
        label: 'Australia'
      },
      {
        value: '2',
        label: 'USA'
      },
      {
        value: '3',
        label: 'UK'
      }
    ]
  },
  gender: {
    value: 'other',
    type: 'select',
    label: 'Gender',
    defaultText: null,
    position: 'right',
    block: 'firstRow',
    order: 1,
    option: [{
        value: 'male',
        label: 'Male'
      },
      {
        value: 'female',
        label: 'Female'
      },
      {
        value: 'other',
        label: 'Other',
        additionalAttr: 'disabled'
      }
    ]
  },
  decision: {
    value: 'not sure',
    type: 'radio',
    label: 'Have you enjoyed it?',
    position: 'left',
    block: 'secondRow',
    order: 1,
    option: [{
        value: 'yes',
        label: 'Yes',
        additionalAttr: 'disabled'
      },
      {
        value: 'no',
        label: 'No',
      },
      {
        value: 'not sure',
        label: 'I am not sure',
      }
    ]
  },
  message: {
    value: null,
    type: 'textarea',
    label: 'Message',
    placeholder: 'type your message',
    position: 'right',
    block: 'secondRow',
    order: 2,
  },
  hobby: {
    value: ['book', 'movie'],
    type: 'checkbox',
    label: 'Hobbies',
    position: 'left',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'travel',
        label: 'Traveling'
      },
      {
        value: 'book',
        label: 'Reading'
      },
      {
        value: 'movie',
        label: 'Watching movie'
      }
    ]
  },
  privacy: {
    value: null,
    type: 'checkbox',
    label: 'Privacy',
    position: 'right',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'agree',
        label: 'I agree'
      }
    ]
  }
}

我的工作代码:

$.ajax({
  url: "https://api.myjson.com/bins/151f9r",
  method: "GET"
})
  .done(function(response) {
    var data = response.fieldset;
    console.log('.......................');
    console.log(data);
  })
  .fail(function(response) {
  	console.log(response);
	});

var formObj = {
  username: {
    value: null,
    type: 'text',
    label: 'Username',
    placeholder: 'Enter username',
    position: 'left',
    block: 'firstRow',
    order: 1,
    additionalClass: 'user'
  },
  password: {
    value: null,
    type: 'password',
    label: 'Password',
    placeholder: 'enter password',
    position: 'left',
    block: 'firstRow',
    order: 2
  },
  country: {
    value: '3',
    type: 'select',
    label: 'Where are you form?',
    //defaultText: 'Choose here',
    position: 'right',
    block: 'firstRow',
    order: 2,
    option: [{
        value: '1',
        label: 'Australia'
      },
      {
        value: '2',
        label: 'USA'
      },
      {
        value: '3',
        label: 'UK'
      }
    ]
  },
  gender: {
    value: 'female',
    type: 'select',
    label: 'Gender',
    defaultText: null,
    position: 'right',
    block: 'firstRow',
    order: 1,
    option: [{
        value: 'male',
        label: 'Male'
      },
      {
        value: 'female',
        label: 'Female'
      },
      {
        value: 'other',
        label: 'Other',
        additionalAttr: 'disabled'
      }
    ]
  },
  decision: {
    value: 'not sure',
    type: 'radio',
    label: 'Have you enjoyed it?',
    position: 'left',
    block: 'secondRow',
    order: 1,
    option: [{
        value: 'yes',
        label: 'Yes',
        additionalAttr: 'disabled'
      },
      {
        value: 'no',
        label: 'No',
      },
      {
        value: 'not sure',
        label: 'I am not sure',
      }
    ]
  },
  message: {
    value: null,
    type: 'textarea',
    label: 'Message',
    placeholder: 'type your message',
    position: 'right',
    block: 'secondRow',
    order: 2,
  },
  vehicle: {
    value: 'car',
    type: 'checkbox',
    label: 'Preferred vehicles',
    position: 'left',
    block: 'secondRow',
    order: 2,
    option: [{
        value: 'car',
        label: 'I like car'
      },
      {
        value: 'bike',
        label: 'I prefer bike'
      },
      {
        value: 'boat',
        label: 'Boat is my favorit'
      }
    ]
  },
  hobby: {
    value: ['book', 'movie'],
    type: 'checkbox',
    label: 'Hobbies',
    position: 'left',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'travel',
        label: 'Traveling'
      },
      {
        value: 'book',
        label: 'Reading'
      },
      {
        value: 'movie',
        label: 'Watching movie'
      }
    ]
  },
  privacy: {
    value: null,
    type: 'checkbox',
    label: 'Privacy',
    position: 'right',
    block: 'secondRow',
    order: 3,
    option: [{
        value: 'agree',
        label: 'I agree'
      }
    ]
  },
  email: {
    value: null,
    type: 'text',
    label: 'Email',
    placeholder: 'Enter email',
    position: 'right',
    block: 'secondRow',
    order: 1,
    additionalAttr: 'disabled',
    additionalClass: 'emailbox custom'
  }
}

console.log(formObj);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
提问于
用户回答回答于

您可以使用reduce循环data遍历数组。检查名称是否作为formObj使用时的键存在hasOwnProperty。如果存在则使用扩展语法来浅层复制对象并覆盖value属性。

var data = [{"name":"username","value":null},{"name":"password","value":null},{"name":"gender","value":"other"},{"name":"country","value":"3"},{"name":"decision","value":"not sure"},{"name":"hobby","value":[]},{"name":"privacy","value":null},{"name":"message","value":null}]
var formObj = {"username":{"value":null,"type":"text","label":"Username","placeholder":"Enter username","position":"left"},"password":{"value":null,"type":"password","label":"Password","placeholder":"enter password","position":"left"},"gender":{"value":"female","type":"select","label":"Gender","defaultText":null,"position":"right"},"country":{"value":"3","type":"select","label":"Where are you form?","position":"right","block":"firstRow"},"decision":{"value":"not sure","type":"radio","label":"Have you enjoyed it?","position":"left","block":"secondRow"},"email":{"value":null,"type":"text","label":"Email","placeholder":"Enter email","position":"right"},"hobby":{"value":[null,null],"type":"checkbox","label":"Hobbies","position":"left","block":"secondRow"},"privacy":{"value":"","type":"checkbox","label":"Privacy","position":"right","block":"secondRow"},"message":{"value":"","type":"textarea","label":"Message","placeholder":"type your message","position":"right"},"vehicle":{"value":"car","type":"checkbox","label":"Preferred vehicles","position":"left","block":"secondRow"}}

var newObj = data.reduce((c, v) => {
  if (formObj.hasOwnProperty(v.name)) c[v.name] = {...formObj[v.name],value: v.value};
  return c;
}, {});

console.log(newObj);
用户回答回答于

一个简单的方法是将此任务分为三个步骤:

  1. formObj通过该Object.entries()方法将键/值条目提取到数组中
  2. 过滤所提取的键/值对从formObj移除条目不具有匹配namedata阵列
  3. newObj通过该reduce()方法构造,以将过滤的键/值条目数组减少为单个对象

这可以通过以下方式实现:

var data = [{"name":"username","value":null},{"name":"password","value":null},{"name":"gender","value":"other"},{"name":"country","value":"3"},{"name":"decision","value":"not sure"},{"name":"hobby","value":[]},{"name":"privacy","value":null},{"name":"message","value":null}]
var formObj = {"username":{"value":null,"type":"text","label":"Username","placeholder":"Enter username","position":"left"},"password":{"value":null,"type":"password","label":"Password","placeholder":"enter password","position":"left"},"gender":{"value":"female","type":"select","label":"Gender","defaultText":null,"position":"right"},"country":{"value":"3","type":"select","label":"Where are you form?","position":"right","block":"firstRow"},"decision":{"value":"not sure","type":"radio","label":"Have you enjoyed it?","position":"left","block":"secondRow"},"email":{"value":null,"type":"text","label":"Email","placeholder":"Enter email","position":"right"},"hobby":{"value":[null,null],"type":"checkbox","label":"Hobbies","position":"left","block":"secondRow"},"privacy":{"value":"","type":"checkbox","label":"Privacy","position":"right","block":"secondRow"},"message":{"value":"","type":"textarea","label":"Message","placeholder":"type your message","position":"right"},"vehicle":{"value":"car","type":"checkbox","label":"Preferred vehicles","position":"left","block":"secondRow"}}

/* Extract key value entries from input object */
const keyValues = Object.entries(formObj)

/* Filter input object entries by existence of 
name matching key from data array */
const filteredKeyValues = keyValues.filter(([key, value]) => {
  return data.findIndex(({ name }) => name === key) !== -1;
});

/* Construct newObj object from filtered key value entries */
const newObj = filteredKeyValues.reduce((result, [key, value]) => {
  return { [key] : value, ...result };
}, {});
  
console.log(newObj)

扫码关注云+社区

领取腾讯云代金券