我的数组,存储在名为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: 'Preffered 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 objects
(数组)中有data
,而10 objects
(对象)中有formObj
。
我的目标是将name
中的data
与formObj
匹配,并仅使用匹配的对象创建新对象。新对象的所有value
应该来自data
(数组)。简而言之,从newObj
创建一个formObj
类似/。 newObj
将不包含不在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: 'Preffered 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>
答案 0 :(得分:2)
您可以使用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":"Preffered 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 :(得分:0)
一种简单的方法是将该任务分为三个步骤:
formObj
方法将Object.entries()
的键/值项提取到数组中formObj
中过滤提取的键/值对,以删除name
数组中不匹配data
的条目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":"Preffered 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)