0

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

4
  • What did you tried ? Commented 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]== 448 Commented Oct 13, 2017 at 7:59
  • Which language is this?? :) Commented Oct 13, 2017 at 8:00
  • I tried in both js and Python This is JS Commented Oct 13, 2017 at 8:01

1 Answer 1

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; }

Sign up to request clarification or add additional context in comments.

2 Comments

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
Mine Pleasure Mam & Yes i found the same Just 1 minute ago Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.