admin 管理员组文章数量: 1086019
I'm trying to filter an array to build another array with the results I get. But the bination of functions I have been using with jQuery haven't workk so far. So I would appreciate some advice about how to solve this.
Here is the example of what I'm trying to implement:
This is my first array:
var data =[salt.1200, salt.1100, salt.1000, salt.0900, salt.0800, salt.0700, current.1200, current.1100, current.1000, current.0900, current.0800, current.0700];
And from this array I would like to build two different arrays, one for salt and one from current.
I have tried to use a for loop with a jQuery grep function on it and split, but that didn't worked.
var plotsArray = [];
for (var i=0; i<data.length; i++){
plotsArray = jQuery.grep(data, function(a){
return (a.split(".") == 'salt')
});
}
console.log('plotsArray: '+plotsArray);
Also, I been trying to do is to read the first part of the string until the dot and then build an array base on that result. Using for example array filter, but I haven't figure it out how to read just until the dot.
I will appreciate any guidance on how to solve this.
Regards,
I'm trying to filter an array to build another array with the results I get. But the bination of functions I have been using with jQuery haven't workk so far. So I would appreciate some advice about how to solve this.
Here is the example of what I'm trying to implement:
This is my first array:
var data =[salt.1200, salt.1100, salt.1000, salt.0900, salt.0800, salt.0700, current.1200, current.1100, current.1000, current.0900, current.0800, current.0700];
And from this array I would like to build two different arrays, one for salt and one from current.
I have tried to use a for loop with a jQuery grep function on it and split, but that didn't worked.
var plotsArray = [];
for (var i=0; i<data.length; i++){
plotsArray = jQuery.grep(data, function(a){
return (a.split(".") == 'salt')
});
}
console.log('plotsArray: '+plotsArray);
Also, I been trying to do is to read the first part of the string until the dot and then build an array base on that result. Using for example array filter, but I haven't figure it out how to read just until the dot.
I will appreciate any guidance on how to solve this.
Regards,
Share Improve this question edited Jan 4, 2013 at 12:58 Probandot asked Jan 4, 2013 at 12:52 ProbandotProbandot 1951 silver badge17 bronze badges 6-
Does
data
contain strings? – pimvdb Commented Jan 4, 2013 at 12:54 - also doesn't a.split() return an array? a.split(".")[0] maybe? – NickSlash Commented Jan 4, 2013 at 12:56
- @pimvdb, data contain strings. – Probandot Commented Jan 4, 2013 at 13:02
- @NickSlash, a.split returns an array with two elements from the string [salt, 1200]. And I want to get an array with all the salt elements. Make sense? – Probandot Commented Jan 4, 2013 at 13:03
- not really no? ([array] == [string]) == false – NickSlash Commented Jan 4, 2013 at 13:07
7 Answers
Reset to default 3I would do something more along the lines off this.
var plotsArray = [];
for (var i=0; i<data.length; i++){
if(data[i].indexOf('salt') >= 0){
plotsArray.push(data[i]);
}
console.log('plotsArray: '+plotsArray);
Make change here -
return (a.split(".")[0] == 'salt')
and your code will work. Working fiddle - http://jsfiddle/Lh8Pt/
Using you code I would do:
var plotsArray = [];
plotsArray.push($.grep(data, function(n,i){
return n.split(".")[0] !== "salt";
}));
And please be sure that the data array contains strings.
I reckon your test should either be:
return a.split(".")[0] == "salt";
or
return a.indexOf("salt") == 0;
The latter might be better because it will not error out in the occasion that one of the array values doesn't contain a dot.
Using filter/map
methods you could do something like:
var current = [],
salt = data.filter(
function(a){
var x = a.split('.');
if (/current/i.test(x[0])) { this.push(x[1]); }
// ^ = current
return /salt/i.test(x[0])
}, current
// ^ context
).map(
function(b){
return b.split('.')[1]}
);
MDN about Array.filter
Try this: http://jsfiddle/afEGC/
var data = ['salt.1200', 'salt.1100', 'salt.1000', 'salt.0900', 'salt.0800', 'salt.0700', 'current.1200', 'current.1100', 'current.1000', 'current.0900', 'current.0800', 'current.0700'];
var asplit = new Array();
asplit['salt'] = new Array();
asplit['current'] = new Array();
jQuery.each(data,
function(index,value)
{
var split_value = value.split('.');
asplit[split_value[0]].push(split_value[1]);
});
console.log('salt: ' + asplit['salt']);
console.log('current: ' + asplit['current']);
var saltArray = [];
var currentArray = [];
for(var i=0;i<data.length;i++){
if(data[i].split('.')[0]=='salt'){
saltArray.push(data[i]);
}else{
currentArray.push(data[i]);
}
}
本文标签: jqueryHow to create an array from elements of another array using javascriptStack Overflow
版权声明:本文标题:jquery - How to create an array from elements of another array using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744008239a2517671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论