首页 > 解决方案 > 需要返回多个数组任何数据类型

问题描述

var a = ["A", "|" , "B" ,"|", [],"|",{obj:"Ashut"},"|",["ashu"]];

我需要这样的输出:

A | B | [] | {obj:"Ashut"} | ["ashu"]

我试过这样:

a.reduce(function(item, index){ return item + index; });

但没有得到正确的输出。

标签: angularreactjsecmascript-6

解决方案


尝试这个 :

// Input array
var a = ["A", "|" , "B" ,"|", [],"|",{obj:"Ashut"},"|",["ashu"]];

// convert objects into string
var b = a.map((item) => {
	if (typeof item === 'object') {
  	item = JSON.stringify(item);
  }
  return item;
});

// join the array
var res = b.join(' ');

// result
console.log(res);


推荐阅读