3

I have the below array

 ["0,5,p1", "24,29,p2", "78,83,p2", "78,83,p3", "162,167,p2" ] 

i want the output as ["5,p1","10,p2","5,p3"] , so p1..3 are video files paying time with start and end time . 0,5 mean p1 profile played for 5 sec and so on. I want to know what profile take what time in total using ECMA script map,reduce function. Here is what i tried but it doesnt work:

 var ca =   uniqueArray.reduce(function(pval, elem) {
    var spl = elem.split(',');
            var difference = Math.round(spl[1] - spl[0]);
            return difference;
    },elem.split(',')[3]);
2
  • 2
    Isn't it 15,p2 ? Commented Jul 26, 2017 at 8:10
  • yes right , i think i missed it..thanks Commented Jul 26, 2017 at 8:14

4 Answers 4

6

I dont think it can be done in one pass, but I could be wrong. I'd go for a 2 step...

  1. Reduce the array to get unique map of pX values
  2. Map the result back to an array in the required format

var input = ["0,5,p1", "24,29,p2", "78,83,p2", "78,83,p3", "162,167,p2" ] 

var step1 = input.reduce(function(p,c){
    var parts = c.split(",");
    if(!p[parts[2]])
       p[parts[2]] = 0;
    p[parts[2]] += parseInt(parts[1],10) - parseInt(parts[0],10);
    return p;
},{});

var result = Object.keys(step1).map(function(e){
    return step1[e] + "," + e;
});

console.log(result);

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

Comments

1

You could use es6 map:

arrayWithNumbers.map(a => {var spl = a.split(','); return (spl[1] - spl[0]) + "," + spl[2]})

3 Comments

Its not fully satisfied .Op need to added the same arguments value with in array
@Jamiec I get ["5,p1", "5,p2", "5,p2", "5,p3", "5,p2"] - Is that not what he wanted?
@BenTaliadoros no. Check the question. He's essentially grouping and summing by pX
1

For a single loop approach, you could use a hash table for same third parts, like 'p1'. If a hash is given, then update the value with the actual delta.

var array = ["0,5,p1", "24,29,p2", "78,83,p2", "78,83,p3", "162,167,p2"],
    hash = Object.create(null),
    result = array.reduce(function(r, a) {
        var parts = a.split(','),
            delta = parts[1] - parts[0],
            key = parts[2];

        if (!(key in hash)) {
            hash[key] = r.push([delta, key].join()) - 1;
            return r;
        }
        r[hash[key]] = [+r[hash[key]].split(',')[0] + delta, key].join();
        return r;
    }, []);

console.log(result);

Comments

0

I have updated the code. Please check now.

    var ca =   ["0,5,p1", "24,29,p2", "78,83,p2", "78,83,p3", "162,167,p2" ] .reduce(function(result, elem) {
        var spl = elem.split(',');
        var difference = Math.round(spl[1] - spl[0]);
        var found = false 
        for (var i = 0 ; i < result.length; i++) {
          if (result[i].split(',')[1] == spl[2]) {
            result[i] = parseInt(result[i].split(',')[0]) + difference+","+spl[2]; 
            found = true;
          }
        }
        if (!found) result.push(difference+","+spl[2]);       
        return result;
        },[]);
        
     console.log("modified array",ca);

2 Comments

Someone put a comment as to where the running code is. You have to click on Run code snippet and the answer comes. In case, the expectation is something else, please post in comment so that I can help.
See OP post for expectations, but you don't meet them :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.