简体   繁体   English

如何从 object 中提取具有 typescript 中的属性和数组的数组?

[英]How to extract an array from an object with property and array in typescript?

I have an object which always have a property and an array.我有一个 object 总是有一个属性和一个数组。 When I print it out with console.log(obj) .当我用console.log(obj)打印出来时。 It likes the following.它喜欢以下内容。

 ProjectName: MyTest1
 [0] { foo: 1, bar: 2}
 [1] { foo: 3, bar: 4}
 [2] { foo: 5, bar: 6} 

Or或者

ProjectName: MyTest2
[0] { foo: 1, bar: 2}
[1] { foo: 3, bar: 4}
[2] { foo: 5, bar: 6} 
[3] { foo: 6, bar: 7}

UPDATE更新

There are hundreds of them from backend, so the array's size is not fixed.后端有数百个,因此数组的大小不是固定的。 But the object is always a single property plus an array.但是 object 始终是单个属性加上一个数组。 This object is a combination of a json object and an array.这个 object 是 json object 和数组的组合。 I want a code to extract it.我想要一个代码来提取它。 Now I want extract the array from the object and assign it to an array variable so I can loop through it.现在我想从 object 中提取数组并将其分配给数组变量,以便循环遍历它。 Which means I must get rid of the first property.这意味着我必须摆脱第一个财产。

UPDATE1更新1

I want the new object is extracted from the original object by code rather than hard code since we have many dynamic size array.我希望通过代码而不是硬代码从原始 object 中提取新的 object,因为我们有许多动态大小的数组。

 [0] { foo: 1, bar: 2}
 [1] { foo: 3, bar: 4}
 [2] { foo: 5, bar: 6}

Try this?尝试这个?

let superArray: [] = [];

const obj = {
     a: [{ foo: 1, bar: 2}],
     b: [{ foo: 3, bar: 4}],
     c: [{ foo: 5, bar: 6}] 
};

for (var array in obj) {
    superArray = superArray.concat(obj[array]);
}

console.log(superArray);

see my comment for "already asked"请参阅我对“已经问过”的评论

for short:简而言之:

var mm=[ 
  { foo: 1, bar: 2} , { foo: 3, bar: 4} ,  { foo: 5, bar: 6} , { foo: 6, bar: 7}
 ];
 mm.property="Hello world";
 console.log("\n1) IsArray ?------------");
 console.log( mm instanceof Array ? "Array":"Other...");
 console.log("2) see mm");
 console.log(mm);


 console.log("\nA------- all properties ---------");
 for (var i in mm) {
  if(mm.hasOwnProperty(i)) {
  console.log("property "+i+"=",mm[i]);
  }
 }


 console.log("\nB------ Array indexes ----------");
 for (var i=0; i<mm.length;i++) {
   console.log("property "+i+"=",mm[i]);
 }

 var mm2=JSON.parse(JSON.stringify(mm));
 mm2[9]="index 10???";
 console.log("\nC------ Array indexes ----------");
 for (var i=0; i<mm2.length;i++) {
   console.log("property "+i+"=",mm2[i]);
 }

output: output:

控制台输出

Advice建议

Avoid mixing properties and array indexes, use a supplementary property to store your array.避免混合属性和数组索引,使用补充属性来存储数组。

let myDatas={
 datas : [{xx:yy},{zz:ff}],
 title : "xxxx"
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从数组中提取 object - typescript - extract object from array - typescript 如何遍历并从复杂对象(数组)中提取属性值? - How to loop through and extract property values from a complex object (Array)? JS-从对象提取Array属性 - JS - extract an Array property from an object Typescript:如何从具有更改属性名称的数组中的 JSON 个对象中提取值? - Typescript: How can I extract the value from JSON objects inside an array with changing property names? 如何将对象的属性值提取到数组中并使数组中的元素不同? - How to extract the property values of a object into an array and make elements in the array different? 如何从 JavaScript 中的嵌套数组对象中提取特定属性作为数组 - How can I extract specific property as an array from nested array object in JavaScript 从 object 中提取数组? - Extract array from object? 从 object javascript/typescript 数组更改属性名称 - Change property name from an array of object javascript/typescript 从动态对象数组中,如何将 mutltple 属性的值提取为数组 - From an array of dynamic objects, how to extract value of a mutltple property as array 使用Javascript从数组或对象中提取value属性 - Extract a value property from either an array OR object using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM