我有一个数组(JSON对象):
result = [[{object},{object}], [{object}], [{object},{object},{object}]]
我正试图把它变成一个对象数组:
array = [{object},{object},{object}],{object},{object},{object}]
然而,无论我尝试哪种方法,我都会从console.log(数组)输出:
[{object}, [circular], {object}, [circular], [circular] etc
我不知道这样的循环是什么?
var array = [];
for (var i=0; i<result.length;i++) {
array = array.concat(result[i]);
}
或:
for (var i=0; i<result.length;i++) {
var res = result[i];
for (var x=0; x<res.length; x++){
array.push(res[x]);
}
}
答案 0 :(得分:2)
你可能需要一些递归函数来推送累加器中的元素。 当它不是一个数组时,只需按下它,否则,推送它的组件:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
at System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
at NUnit.Engine.Drivers.NUnit3FrameworkDriver.Load(String testAssemblyPath, IDictionary`2 settings)
at NUnit.Engine.Runners.DirectTestRunner.LoadPackage()
at NUnit.Engine.Runners.AbstractTestRunner.Load()
at NUnit.Engine.Runners.MasterTestRunner.LoadPackage()
at NUnit.Engine.Runners.AbstractTestRunner.EnsurePackageIsLoaded()
at NUnit.Engine.Runners.MasterTestRunner.NUnit.Engine.ITestRunner.Explore(TestFilter filter)
at JetBrains.ReSharper.UnitTestRunner.nUnit30.BuiltInNUnitRunner.<>c__DisplayClass1.<RunTests>b__0() in c:\Build Agent\work\10282fe47e6c6213\Psi.Features\UnitTesting\nUnit\Runner30\Src\BuiltInNUnitRunner.cs:line 79
at JetBrains.ReSharper.UnitTestRunner.nUnit30.BuiltInNUnitRunner.WithExtensiveErrorHandling(IRemoteTaskServer server, Action action) in c:\Build Agent\work\10282fe47e6c6213\Psi.Features\UnitTesting\nUnit\Runner30\Src\BuiltInNUnitRunner.cs:line 623
答案 1 :(得分:1)
如果初始数组仅包含2个级别作为示例(数组中没有元素是数组),这将起作用:
var array = result.reduce(function(a,b){
return a.concat(b);
});
使用 reduce 和 concat 可以解决问题,将所有元素都返回到一个数组中。
答案 2 :(得分:0)
如果我理解你的问题,你需要这样的事情:
var arrays = [[{a:1},{b: 2}], [{c:3}], [{d: 4},{e: 5},{f: 6}]];
function flattenArray(arr, result) {
var res = result || [];
arr.forEach(function(item){
if (Array.isArray(item)) {
flattenArray(item, res)
} else {
res.push(item)
}
})
return res;
}
console.log(flattenArray(arrays))
&#13;
答案 3 :(得分:0)
array = [ [object,object], [object], [object,object,object] ]
_.flattenDepth(array, 1);