Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中将对象转换为包含多个对象的数组的理想方法是什么?(ES6允许)_Javascript_Arrays_Vue.js_Ecmascript 6 - Fatal编程技术网

在Javascript中将对象转换为包含多个对象的数组的理想方法是什么?(ES6允许)

在Javascript中将对象转换为包含多个对象的数组的理想方法是什么?(ES6允许),javascript,arrays,vue.js,ecmascript-6,Javascript,Arrays,Vue.js,Ecmascript 6,我有一个这样的目标: foo: { someGuid: someString, someGuid: someString, someGuid: someString } 我试图找出将其作为id/值对的对象数组插入现有对象的最佳方法: bar: { someOtherInfo: someOtherValue, someOtherInfo: someOtherValue, baz: [ { id: someGuid, value: someS

我有一个这样的目标:

foo: {
  someGuid: someString,
  someGuid: someString,
  someGuid: someString
}
我试图找出将其作为id/值对的对象数组插入现有对象的最佳方法:

bar: {
  someOtherInfo: someOtherValue,
  someOtherInfo: someOtherValue,
  baz: [
    {
      id: someGuid,
      value: someString
    },
    {
      id: someGuid,
      value: someString
    },
    {
      id: someGuid,
      value: someString
    }
  ]
}
我真的很想找出实现这一目标的最佳方法。所谓“最佳”,我指的是代码清晰性/简单性和性能之间的良好平衡(如果可能的话,也可以指上述所有方面)

我知道有多种方法可以用来转换这些数据,所以我真的很好奇其他人想出了什么,我觉得这对我自己和其他人来说都是一个很好的学习机会

此外,这是在Vue应用程序中,如果这有区别的话。(我不认为有一种特定于vue的方法可以做到这一点,但如果有,我肯定有兴趣了解如何做到这一点!)


到目前为止,我提出的方法还没有奏效,但这里是为那些感兴趣的人准备的。我相信我完全弄错了(可能是把它复杂化了)

我的数据:

originalObject: {
  someGuid: someString,
  someGuid: someString,
  someGuid: someString
}

newObject: {
        some: null,
        other: '1',
        stuff: null,
        irrelevant: null,
        toThis: null,
        problem: null,
        targetArray: [
          {},
        ],
      },
我的逻辑是:

someFunction() {
  const foo = this.originalObject;
  const bar = this.newObject.targetArray;
  const fooLength = Object.keys(foo).length;

  Object.keys(foo).forEach(function (key) {
    for (let i = 0; i < fooLength; i++) {
      const foobar = bar[i];
      console.log(foobar.ItemAttributeId);
      console.log(foobar.Value);
      foobar.ItemAttributeId = key;
      foobar.Value = foo[key];

      bar.push({ ItemAttributeId: foobar.ItemAttributeId, Value: foobar.Value });
    }
  });
},
someFunction(){
const foo=this.originalObject;
常量条=this.newObject.targetArray;
const-wouldength=Object.keys(foo.length);
Object.keys(foo.forEach)函数(key){
for(设i=0;i<傻瓜长度;i++){
常数foobar=bar[i];
log(foobar.ItemAttributeId);
console.log(foobar.Value);
foobar.ItemAttributeId=键;
foobar.Value=foo[key];
push({ItemAttributeId:foobar.ItemAttributeId,Value:foobar.Value});
}
});
},

所以我觉得我把事情弄得比它需要的更复杂了。此外,结果输出是不正确的(我不知何故多次返回该值,但它似乎是一个随机数的倍数…有时返回4次,有时返回3次,我试图解释原因)。我觉得有更好的开始方式,但我错过了,现在我陷入了困境。

在Vue中,您可以使用计算属性

console.clear()
新Vue({
el:“应用程序”,
数据:{
傅:{
someGuid1:“someString”,
someGuid2:“someString”,
someGuid3:“someString”
}
},
计算:{
baz(){
//这里没什么特别的——只是javascript
返回Object.entries(this.foo).map(([key,val])=>({id:key,value:val}))
}
}
})

{{baz}}

在Vue中,您将使用计算属性

console.clear()
新Vue({
el:“应用程序”,
数据:{
傅:{
someGuid1:“someString”,
someGuid2:“someString”,
someGuid3:“someString”
}
},
计算:{
baz(){
//这里没什么特别的——只是javascript
返回Object.entries(this.foo).map(([key,val])=>({id:key,value:val}))
}
}
})

{{baz}}

您可以使用
数组。减少
对象。键

this.newObject.targetArray = Object.keys(this.originalObject).reduce((array, key) => {
  array.push({
    id: key,
    value: this.originalObject[key],
  })

  return array
},[])

您可以使用
Array.reduce
Object.keys

this.newObject.targetArray = Object.keys(this.originalObject).reduce((array, key) => {
  array.push({
    id: key,
    value: this.originalObject[key],
  })

  return array
},[])
我相信有两种“流行”的方法可以做到这一点

// You have this
const input = {
    foo: {
        someGuid1: "someString1",
        someGuid2: "someString2",
        someGuid3: "someString3"
    }
}

// You want this:
[
    {id: "someGuid1", value: "someString1"},
    {id: "someGuid2", value: "someString2"},
    {id: "someGuid3", value: "someString3"}
];
当务之急是:

const baz = [];

for (const [id, value] of Object.entries(input.foo)) {
    baz.push({ id, value });
}
功能方式:

const baz = Object.entries(input.foo).map(([id, value]) => ({ id, value }));
在我看来,如果有代码库,您应该始终遵循您正在使用的代码库的样式,如果没有现有代码,则应该遵循您感觉更舒适的代码库样式。

我相信有两种“流行”的方法可以做到这一点

// You have this
const input = {
    foo: {
        someGuid1: "someString1",
        someGuid2: "someString2",
        someGuid3: "someString3"
    }
}

// You want this:
[
    {id: "someGuid1", value: "someString1"},
    {id: "someGuid2", value: "someString2"},
    {id: "someGuid3", value: "someString3"}
];
当务之急是:

const baz = [];

for (const [id, value] of Object.entries(input.foo)) {
    baz.push({ id, value });
}
功能方式:

const baz = Object.entries(input.foo).map(([id, value]) => ({ id, value }));

在我看来,如果有代码库,您应该始终遵循您正在使用的代码库的样式,如果没有现有代码,则应该遵循您感觉更舒适的代码库样式。

“我知道有多种方法可以转换此数据”-您想出了哪些方法?到目前为止,您已经完成了哪些工作?您试图用它解决的具体问题是什么?打个招呼。对你来说,什么是“良好的平衡”?对我来说,这里的“很好的学习机会”是让你去弄清楚的。
Object.entries
.map
那个?或者
Object.keys().map()
,无论如何,某种形式的
将对象转换为数组,并为foo对象的每个属性返回一个新对象作为旁注。。。一个相当常见的模式是[to | from}pairs函数,它将对象转换为数组数组:
[[someGuid,someString],[someGuid,someString],[someGuid,someString]]
另请参见-SO不是一项代码编写服务,作为一名高级开发人员,您至少应该能够展示此类问题的当前实现,并描述良好的外观。“我知道有多种方法可用于转换此数据”-你想出了什么方法?到目前为止你完成了什么,你试图解决的具体问题是什么?给出一个答案。对你来说,“良好的平衡”到底是什么?对我来说,这里的“良好的学习机会”是让你弄清楚的。
Object.entries
.map
那个?或者
Object.keys().map()
,无论如何,某种形式的
将对象转换为数组,并为foo对象的每个属性返回一个新对象作为旁注。
这方面的一个相当常见的模式是一个[to | from}对函数,它将对象转换为数组:
[[someGuid,someString],[someGuid,someString],[someGuid,someString]]
另请参见-SO不是一项代码编写服务,作为高级开发人员,您至少应该能够展示此类问题的当前实现,并描述良好的外观。