简体   繁体   English

将对象组合到 Typescript 中的对象数组中?

[英]Combine objects in an array of objects in Typescript?

i am having trouble combining objects from an array of Objects in typescript.我在组合 typescript 中对象数组中的对象时遇到问题。

the Array looks like that:数组看起来像这样:

0: {type: 'FeatureCollection', features: Array(134)}
1: {type: 'FeatureCollection', features: Array(109)}

what i need is an object (no array) with all "features" combined - so that it looks like this:我需要的是结合了所有“功能”的 object(无数组)——因此它看起来像这样:

{type: 'FeatureCollection', features: Array(243)}

i am very new to typescript so sorry for that probably easy question...我是 typescript 的新手,很抱歉这个可能很简单的问题......

Thank you so much!!太感谢了!!

EDIT: maybe the question was not too good to understand.编辑:也许这个问题不太好理解。 Hope this helps: Array(134) means there are 134 Objects inside.希望这会有所帮助:Array(134) 意味着里面有 134 个对象。 this does what i need manually when the array of objects (collection) is 2 long:当对象数组(集合)长 2 时,这会手动执行我需要的操作:

  const result = [...collection[0].features, ...collection[1].features];
  const resultCollection: FeatureCollection = collection[0];
  resultCollection.features = result;

i need to make this work for any length of collection.我需要为任何长度的收藏制作这项工作。

Are you looking for something like this?你在找这样的东西吗? You'll need to add types, but you can merge the features arrays from all entries in the data array using vanilla JS methods.您需要添加类型,但您可以使用原始 JS 方法从data数组中的所有条目合并features arrays。

  1. Create the output object and specify its type .创建output object 并指定其type
  2. Use Array#filter to select the entries in data with the matching type.使用Array#filter到 select data中具有匹配类型的条目。
  3. Use Array#flatMap to select the features array from each of the entries above and project their contents into a single array.使用Array#flatMap到 select 上面每个条目的features数组,并将它们的内容投影到一个数组中。

 const data = [ { type: 'FeatureCollection', features: [1, 2, 3] }, { type: 'FeatureCollection', features: [4, 5, 6] } ]; const output = { type: 'FeatureCollection', features: data.filter(obj => obj.type === 'FeatureCollection').flatMap(obj => obj.features) }; console.dir(output);

With the reduce function:随着减少 function:

const array = [
  {type: 'FeatureCollection', features: [1, 2, 3]},
  {type: 'FeatureCollection', features: [4, 5, 6]},
  {type: 'Test', features: [4, 5, 6]}
]

const merged = array.reduce((array, item) => {
  const found = array.find((i) => i.type === item.type);
  if (found) {
    found.features.push(...item.features);
  } else {
    array.push(item);
  }
  return array;
}, []);

console.log(merged);

---- EDITED ---- ---- 编辑 ----

option 2:选项 2:

const array = [
  {type: 'FeatureCollection', features: [1, 2, 3]},
  {type: 'FeatureCollection', features: [4, 5, 6]},
  {type: 'Test', features: [4, 5, 6]}
]

const merged = array.reduce((obj, item) => {
  if (obj[item.type]) {
    obj[item.type].push(...item.features);
  } else {
    obj[item.type] = item.features;
  }
  return obj;
}, {} as {[key: string]: number[]});

const keys = Object.keys(merged);
const result = keys.map((k) => ({type: k, features: merged[k]}));

console.log(result);

Just use a simple Array.prototype.reduce function and the spread operator只需使用一个简单的Array.prototype.reduce function 和扩展运算符

 const original = [ { type: "banana", features: ["34", "wow", "hotdog"] }, { type: "banana", features: ["dog", "cat", "recursion"] }, ]; const compress = (original) => original.reduce((acc, cur) => { acc.features = [...(acc.features?? []), ...cur.features]; return acc; }, { type: (original[0].type) }); console.log(compress(original));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM