i have array in this form
['203,448', '204,297', '204,448', '205,297', '230,448', '231,297', '24,448', '24,297','203,548', '204,548', '204,548' ]
Desire Output :
0:['203,448', '204,448', '230,448','24,448', ]
1: [ '204,297', '205,297', '231,297', '24,297']
2: ['203,548', '204,548', '204,548']
I want to sepreate Elements on the base of 2 featur i.e 203,448 and 204 ,297
cs95
406k106 gold badges745 silver badges798 bronze badges
asked Oct 13, 2017 at 7:54
Mohammad Ahmad Shabbir
1431 silver badge13 bronze badges
-
What did you tried ?Serge K.– Serge K.2017年10月13日 07:56:01 +00:00Commented Oct 13, 2017 at 7:56
-
i try to different using 2nd Feature foreach feature in layer_featureinfo if features_result[1]== 297 : else if features_result[1]== 448Mohammad Ahmad Shabbir– Mohammad Ahmad Shabbir2017年10月13日 07:59:25 +00:00Commented Oct 13, 2017 at 7:59
-
Which language is this?? :)Gautam– Gautam2017年10月13日 08:00:54 +00:00Commented Oct 13, 2017 at 8:00
-
I tried in both js and Python This is JSMohammad Ahmad Shabbir– Mohammad Ahmad Shabbir2017年10月13日 08:01:24 +00:00Commented Oct 13, 2017 at 8:01
1 Answer 1
You could take a hash table for same second part of the string and collect same items in an array.
var data = ['203,448', '204,297', '204,448', '205,297', '230,448', '231,297', '24,448', '24,297', '203,548', '204,548', '204,548'],
hash = Object.create(null),
result = data.reduce(function (r, a) {
var key = a.split(',')[1];
if (!hash[key]) {
hash[key] = [];
r.push(hash[key]);
}
hash[key].push(a);
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
For putting only the first part, you could use splitting array
var data = ['203,448', '204,297', '204,448', '205,297', '230,448', '231,297', '24,448', '24,297', '203,548', '204,548', '204,548'],
hash = Object.create(null),
result = data.reduce(function (r, a) {
var s = a.split(',');
if (!hash[s[1]]) {
hash[s[1]] = [];
r.push(hash[s[1]]);
}
hash[s[1]].push(s[0]);
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered Oct 13, 2017 at 8:01
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Mohammad Ahmad Shabbir
Thanks A lot Can You guide lets say if we want Result holds [ [ "203", "204", "230", "24" ], [ "204", "205", "231", "24" ], [ "203", "204", "204" ] ] I try but i not work I may use this in some other case
Mohammad Ahmad Shabbir
Mine Pleasure Mam & Yes i found the same Just 1 minute ago Thanks
lang-js