将对象中的所有数组合并为一个数组。

Hal*_*ted 2 javascript arrays google-chrome google-chrome-extension

我正在为 Google Chrome 开发一个扩展,我想将某个对象中的所有数组合并到一个数组中,而不是将它们拆分。所以现在,我的控制台 o

chrome.storage.sync.get(null, function(all) {
// this returns everything in chrome's storage. 
}
Run Code Online (Sandbox Code Playgroud)

在我的控制台中它看起来像这样:

在此输入图像描述

但是,我希望它实际上将所有数组合并为一个,如下所示:

目的

feed_0:数组[364]

我试过这个:

    chrome.storage.sync.get(null, function(all) {
  var test = {}; test = all;
  delete test['currently.settings'];

console.log(test);

var alpha = [];

var result = 0;
  for(var prop in test) {
    if (test.hasOwnProperty(prop)) {
       var second = alpha.concat(prop);

      console.log(second);
    // or Object.prototype.hasOwnProperty.call(obj, prop)
      result++;
    }
  }

   });
Run Code Online (Sandbox Code Playgroud)

但这会返回这个:

在此输入图像描述

tri*_*cot 6

以下是如何从all对象获取一个数组:

var test = Object.values(all).flat();
Run Code Online (Sandbox Code Playgroud)

在不支持这些函数的较旧 JavaScript 版本中,请使用:

var test = Object.keys(all).reduce( (acc, a) => acc.concat(all[a]), [] );
Run Code Online (Sandbox Code Playgroud)

将其分配给feed_0属性当然不是困难的:

test = { feed_0: test };
Run Code Online (Sandbox Code Playgroud)