最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Combine objects in an array of objects in Typescript? - Stack Overflow

programmeradmin4浏览0评论

i am having trouble bining objects from an array of Objects in 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" bined - so that it looks like this:

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

i am very new to typescript so sorry for that probably easy question...

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. this does what i need manually when the array of objects (collection) is 2 long:

  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.

i am having trouble bining objects from an array of Objects in 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" bined - so that it looks like this:

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

i am very new to typescript so sorry for that probably easy question...

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. this does what i need manually when the array of objects (collection) is 2 long:

  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.

Share edited Mar 22, 2022 at 15:02 pcace asked Mar 22, 2022 at 14:48 pcacepcace 6708 silver badges23 bronze badges 3
  • What does Array(134) mean? Is that meant to be a number[] array containing a single-element value of 134? Or do you mean an array (unknown[] or any[]) that contains 134 elements? – Dai Commented Mar 22, 2022 at 14:53
  • Just those 2 elements? What about other values for type: besides 'FeatureCollection'? – Dai Commented Mar 22, 2022 at 14:54
  • Array(134) means there are 134 Objects inside. this does what i need manually when the array of objects (collection) is 2 long: 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. – pcace Commented Mar 22, 2022 at 14:57
Add a ment  | 

3 Answers 3

Reset to default 5

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.

  1. Create the output object and specify its type.
  2. Use Array#filter to select the entries in data with the matching type.
  3. Use Array#flatMap to select the features array from each of the entries above and project their contents into a single array.

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);

Just use a simple Array.prototype.reduce function and the spread operator

const original = [
  { type: "banana", features: ["34", "wow", "hotdog"] },
  { type: "banana", features: ["dog", "cat", "recursion"] },
];

const press = (original) => original.reduce((acc, cur) => {
  acc.features = [...(acc.features ?? []), ...cur.features];
  return acc;
}, { type: (original[0].type) });

console.log(press(original));

With the reduce 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 :

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);
发布评论

评论列表(0)

  1. 暂无评论