将数组列入Javascript数组?

时间:2014-04-21 07:35:47

标签: javascript arrays list

目标:

将以下推文传递给可视化工具,并在用户界面上显示推文。

设定:

我的UI上有以下“列表”。我只想将此列表转换为javascript数组(以便我可以将其传递给可视化工具)。我已经为每个字段的结尾添加'qqq'以确定它的结束位置。

鸣叫:

[*high-pitched yelp* http://t.co/qaluND2Lu3qqq, neutralqqq, 0qqq,
 I checked in at Starbucks on #Yelp http://t.co/8wRVos8STjqqq, negativeqqq, -0.159316qqq,
 i would like to thank yelp for not helping me find any club around santa monica that plays progressive edm / progressive tranceqqq, positiveqqq, 0.372338qqq,
 Nice long bar table & upstairs option (@ Social Kitchen & Brewery) on #Yelp http://t.co/uhQB003NTiqqq, positiveqqq, 0.567625qqq]

问题: 如何按'qqq'拆分,然后将其放入javascript数组?

我尝试过以下操作:

var str = "*high-pitched yelp* http://t.co/qaluND2Lu3qqq, neutralqqq, 0qqq";
var res = str.split("qqq");

但是该方法在每个qqq的末尾添加了一个逗号(,)。我很困惑。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:-2)

更新2

最好只构建一个新的干净数组,没有任何未定义的值。

var str = "*high-pitched yelp* http://t.co/qaluND2Lu3qqq, neutralqqq, 0qqq",
    myArray = str.split('qqq'),
    newArray = new Array();

for ( i = 0; i < myArray.length; i++ ) { // Assuming array is built starting with the key 0
    // Denote any array element that is undefined, or nonsensical white-space
    if ( myArray[i] !== "" && myArray[i] !== undefined && myArray[i] !== " " ) {
        newArray.push(myArray[i]);
    }
}

myArray = newArray;

console.log(myArray);