简体   繁体   English

不想用Javascript打印Array of Array

[英]Don't want to print Array of Array in Javascript

I have an Array of Array and I just want to print outer Array not inner Array. 我有一个数组数组 ,我只想打印外部数组而不是内部数组。

For example my array is :- 例如我的数组是:

[
"Stories",
"Tasks",
"In Progress",
"In Review",
"Completed",
[
{
    "divName": "content-container2",
    "content": "us 2345",
    "topPos": 109,
    "leftPos": 150
},
{
    "divName": "content-container3",
    "content": "Description",
    "topPos": 98,
    "leftPos": 382
},
{
    "divName": "content-container4",
    "content": "12212",
    "topPos": 110,
    "leftPos": 644
}
]
]

I just want to show ["Stories", "Tasks", "In Progress", "In Review", "Completed"], nothing else. 我只想显示[“故事”,“任务”,“进行中”,“正在审查”,“完成”],仅此而已。

Please suggest how to handle this thing in javascript? 请建议如何使用javascript处理此问题?

While iterating the array , check the type of each item in it like 在迭代array ,检查其中每个项目的type

for (var i =0; i< arr.length; i++) {
        if (typeof arr[i] === "string") {
          console.log(arr[i]);
        }
 }

A better approach (inspired from this answer ) 更好的方法(从此答案中得到启发)

for (var i =0; i< arr.length; i++) {
    if( Object.prototype.toString.call( arr[i] ) !== '[object Array]' ) {
       console.log(arr[i]);
}

You can loop through the array and check whether each value is an array or not using JavaScript's instanceof operator . 您可以遍历数组,并使用JavaScript的instanceof运算符检查每个值是否为数组。

var array = [],  // This is your array
    result = []; // This is the result array

// Loop through each index within our array
for (var i = 0; i < array.length; i++)
    /* If the value held at the current index ISN'T an array
     * add it to our result array. */
    if (!(array[i] instanceof Array))
        result.push(array[i]);

// Log the result array
console.log(result);

JSFiddle demo . JSFiddle演示

> ["Stories", "Tasks", "In Progress", "In Review", "Completed"] 

In more modern browsers, this also works: 在更现代的浏览器中,这也适用:

array.filter(function(item){
  return typeof(item) !== "object";
});

Very simple, with three lines you can filter the array: 非常简单,只需三行就可以filter数组:

// Arr is your Array :)
var result = arr.filter(function(value){
  return typeof value != 'array' && typeof value != 'object';
});

// It shows ["Stories", "Tasks", "In Progress", "In Review", "Completed"]
console.log(result); 

See the jsfiddle: http://jsfiddle.net/j4n99uw8/1/ . 参见jsfiddle: http : //jsfiddle.net/j4n99uw8/1/

UPDATED : Also you can extends the Array and use in another sides: 更新 :您还可以扩展Array并在另一侧使用:

Array.prototype.oneDimension = function(){
   return this.filter(function(value){
     return typeof value != 'array' && typeof value != 'object';
   });
};

// In each array you can use it:
console.log( arr.oneDimension() );
console.log( ['9',['9'],['2']].oneDimension() ); // Only contains '9'.

See this jsfiddle: http://jsfiddle.net/j4n99uw8/2/ 看到这个jsfiddle: http : //jsfiddle.net/j4n99uw8/2/

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

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