Javascript 用于重新创建对象数组的方法返回重复的相同对象数组

Javascript 用于重新创建对象数组的方法返回重复的相同对象数组,javascript,arrays,object,Javascript,Arrays,Object,我正在处理一个函数,该函数将获取一个扁平嵌套对象数组,然后返回另一个数组,并重命名对象属性 例如,此输入: [ ] 应返回: [ ] 迄今为止的守则: function format_object(myobj){ var raw_result = []; //the final variable - an array of objects var raw_obj = {}; //every object is kept here temporarly var depth

我正在处理一个函数,该函数将获取一个扁平嵌套对象数组,然后返回另一个数组,并重命名对象属性

例如,此输入:

[

]

应返回:

[

]

迄今为止的守则:

function format_object(myobj){

    var raw_result = []; //the final variable - an array of objects
    var raw_obj = {}; //every object is kept here temporarly
    var depth = 0; //depth of the attribute name

    for(var i = 0; i< myobj.length; i++){ //for each object
        for(var attributename in myobj[i]){ //for each attribute
            depth = attributename.split(".").length-1; //calculate name depth
            if(depth == 0){ 
                raw_obj[attributename] = myobj[i][attributename]; //for simple attribute names, just copy them on the temp object
            }
            else{
                new_attribute = attributename.split('.')[depth] //for complex names, split last word
                raw_obj[new_attribute] = myobj[i][attributename];
            }
        }
        raw_result.push(raw_obj); //add the object we just created into the final variable
    }
    return raw_result;
}
函数格式\u对象(myobj){
var raw_result=[];//最后一个变量-对象数组
var raw_obj={};//每个对象都临时保存在这里
var depth=0;//属性名的深度
对于每个对象(var i=0;i

打印我创建的原始对象,我会在每次迭代中得到正确的对象。但是,最终变量仅由第一个对象组成,重复n次

在循环的每次迭代中都需要一个新的
raw_对象
,否则会不断更改同一对象的属性

将对象推送到数组时,它推送的是引用,而不是副本

因此,您将得到一个数组,其中每个元素都是对完全相同的单个对象的引用

var raw_result = []; //the final variable - an array of objects   
var depth = 0; //depth of the attribute name

for(var i = 0; i< myobj.length; i++){ //for each object

    var raw_obj = {}; // new object
    .....
var原始结果=[]//最后一个变量-对象数组
var深度=0//属性名的深度
对于每个对象(var i=0;i
我认为使用和方法可以更简单

var数据=[
{
id:13,
学生姓名:“约翰”,
“父母,母亲”:“米娅”,
“父母,父亲”:“米洛”,
“父母、母亲、母亲教育”:“理学硕士”,
“父母、父亲、父亲教育”:“学士”,
}, {
id:13,
学生姓名:“艾丽卡”,
“父母。母亲”:“Lea”,
“父母,父亲”:“西奥”,
“父母、母亲、母亲教育”:“学士”,
“父母、父亲、父亲教育”:“高中”,
}
]
var res=data.map(函数(obj){
返回Object.keys(obj).reduce(函数(o,k){
o[k.split('.').pop()]=obj[k];
返回o;
}, {});
})

console.log(res);
您可以迭代数组,获取对象的键,拆分它们,并获取新对象的最后一项和值

var data=[{id:13,‘学生名’:‘约翰’,‘父母.母亲’:‘米亚’,‘父母.父亲’:‘米洛’,‘父母.母亲.母亲.母亲教育’:‘msc’,‘父母.父亲.父亲教育’:‘学士’,},{id:13,'学生姓名':'艾丽卡','父母.母亲':'利亚','父母.父亲':'西奥','父母.母亲.母亲教育':'学士','父母.父亲.父亲教育':'高中',
newArray=data.map(函数(o){
返回Object.keys(o).reduce(函数(r,k){
r[k.split('.').pop()]=o[k];
返回r;
}, {});
});
console.log(newArray);
。作为控制台包装{最大高度:100%!重要;顶部:0;}
function format_object(myobj){

    var raw_result = []; //the final variable - an array of objects
    var raw_obj = {}; //every object is kept here temporarly
    var depth = 0; //depth of the attribute name

    for(var i = 0; i< myobj.length; i++){ //for each object
        for(var attributename in myobj[i]){ //for each attribute
            depth = attributename.split(".").length-1; //calculate name depth
            if(depth == 0){ 
                raw_obj[attributename] = myobj[i][attributename]; //for simple attribute names, just copy them on the temp object
            }
            else{
                new_attribute = attributename.split('.')[depth] //for complex names, split last word
                raw_obj[new_attribute] = myobj[i][attributename];
            }
        }
        raw_result.push(raw_obj); //add the object we just created into the final variable
    }
    return raw_result;
}
var raw_result = []; //the final variable - an array of objects   
var depth = 0; //depth of the attribute name

for(var i = 0; i< myobj.length; i++){ //for each object

    var raw_obj = {}; // new object
    .....
// iterate over the object array
var res = data.map(function(obj) {
  // get all keys from object and iterate over the object 
  // to generate the updated object
  return Object.keys(obj).reduce(function(o, k) {
    // define the object property by splitting property name
    // and getting the last part
    o[k.split('.').pop()] = obj[k];
    // return the object reference
    return o;
    // define initial value as an empty object
  }, {});
})