0

在 javascript/jquery 我如何实现以下

old_dataset = [
{
    "dob": "xyz",
    "name": {
        "first": " abc",
        "last": "lastname"
    },
    "start_date": {
        "moth": "2",
        "day": "5",
        "year": 1
    },
    "children": [
        {
            "child": {
                "id": "1",
                "desc": "first child"
            }
        },
        {
            "child": {
                "id": "2",
                "desc": "second child"
            }
        }
    ]
},
{
    "dob": "er",
    "name": {
        "first": " abc",
        "last": "txt"
    },
    "start_date": {
        "moth": "2",
        "day": "5",
        "year": 1
    },
    "children": [
        {
            "child": {
                "id": "1",
                "desc": "first child"
            }
        },
        {
            "child": {
                "id": "2",
                "desc": "second child"
            }
        }
    ]
}

]

使用 jquery 遍历上述内容并更改为以下内容

new_dataset = [
{
    "dob":"xyz",
    "name": <first and last name values>
    "start_date":<value of month day year>,
    "children": [ {
       child_id :1,
       child_id : 2
      }, 
    ]
 },{
    "dob":"er",
    "name": <first and last name values>
    "start_date":<value of month day year>,
    "children": [ {
       child_id :1,
       child_id : 2
      }, 
    ]
}]

如果有人可以提供代码来转换数据,它将帮助我理解迭代

4

2 回答 2

0

您可以执行以下操作:

function transformDataset(oldDataset) {
  var newDataset = [];
  var newObj;

  for (var i = 0; i < oldDataset.length; i++) {
    newObj = transformObj(oldDataset[i]);
    newDataset.push(newObj);
  }

  return newDataset;
}

function transformObj(obj) {
  var children = obj.children;

  obj.name = obj.name.first + ' ' + obj.name.last;
  obj.start_date = obj.start_date.month + ' ' + obj.start_date.day + ' ' + obj.start_date.year;

  obj.children = [];
  for (var i = 0; i < children.length; i++) {
    obj.children.push(children[i].child.id);
  }

  return obj;
}

var new_dataset = transformDataset(old_dataset);

请注意, new_dataset 将具有一个子 ID数组,而不是具有多个child_id属性的对象。old_dataset.start_date.month您在(已写)中也有错字moth(或者可能是故意的)。

于 2013-02-22T16:23:41.930 回答
0

首先使用 map 迭代数组数据(old_dataset),将元素名称和 start_date 替换为新值,然后返回数组

const old_dataset = [
{
    "dob": "xyz",
    "name": {
        "first": " abc",
        "last": "lastname"
    },
    "start_date": {
        "moth": "2",
        "day": "5",
        "year": 1
    },
    "children": [
        {
            "child": {
                "id": "1",
                "desc": "first child"
            }
        },
        {
            "child": {
                "id": "2",
                "desc": "second child"
            }
        }
    ]
},
{
    "dob": "er",
    "name": {
        "first": " abc",
        "last": "txt"
    },
    "start_date": {
        "moth": "2",
        "day": "5",
        "year": 1
    },
    "children": [
        {
            "child": {
                "id": "1",
                "desc": "first child"
            }
        },
        {
            "child": {
                "id": "2",
                "desc": "second child"
            }
        }
    ]
}
]

let new_dataset = old_dataset.map((arr) => {
  arr.name = `${arr.name.first} ${arr.name.last}`
  arr.start_date = `${arr.start_date.moth} ${arr.start_date.day} ${arr.start_date.year}`
  return arr
})

console.log(new_dataset)

于 2021-05-25T09:36:00.127 回答