4

I need to obtain from a given array of objects another array of objects but a little bit processed. Example:

 var arr =   [
      {
        "level": "INF",
        "model": "A"
      },{
        "level": "INF",
        "model": "B"
      },{
        "level": "INF",
        "model": "C"
      },{
        "level": "INC",
        "model": "A"
      },{
        "level": "IND",
        "model": "A"
      },{
        "level": "IND",
        "model": "B"
      }
]

process_array(arr)

should return:

      [{
        "level": "INF",
        "model": "A-B-C"
      },{
        "level": "INC",
        "model": "A"
      },{
        "level": "IND",
        "model": "A-D"
      }]

I mean, there only will be one object per different level and all the different models for that level will be concatenated by '-'.

What would be an easy way to achieve so?

My approach, for getting an array with different levels:

function process_array(array) {
    var values_seen = {}; // for removing duplicates
    for (var i = 0; i < array.length; i++) {
        values_seen[array[i]["level"]] = true;
    }
    return Object.keys(values_seen);
}

Now i need to obtain the concatented models for each levels..

2
  • You should add the code you've already tried to your question. Commented Apr 9, 2015 at 11:32
  • shortly edited with how I have started... Commented Apr 9, 2015 at 11:38

4 Answers 4

4

Just reduce the array into an object:

return array.reduce(function(total, current) {
  if (total[current.level]) { // existing
    total[current.level].model += '-' + current.model;
  } else { // new
    total[current.level] = current;
  }
  return total;
}, {});

Demo

In each iteration you modify the total object and then return it to be used within the next iteration. The final value of that object is returned to the caller.

See also Array.prototype.reduce()

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

1 Comment

Smart! Didn't think of using reduce like that.
2

var arr =   [
      {
        "level": "INF",
        "model": "A"
      },{
        "level": "INF",
        "model": "B"
      },{
        "level": "INF",
        "model": "C"
      },{
        "level": "INC",
        "model": "A"
      },{
        "level": "IND",
        "model": "A"
      },{
        "level": "IND",
        "model": "B"
      }
]

function processArray(array) {
    
    var count = array.length, i, item, result = [], temp = {};
    
    for (i = 0; i < count; i++) {
        item = array[i];
      
        if (temp[item.level] !== undefined) {
            result[temp[item.level]].model += '-' + item.model;        
        } else {
            temp[item.level] = result.length;
            result.push(item);
        }
        
    }

    return result;
}

alert(JSON.stringify(processArray(arr)));

Comments

0

What you are referring to is also called projection.

This code uses EcmaScript 5.

var arr =   [
      {
        "level": "INF",
        "model": "A"
      },{
        "level": "INF",
        "model": "B"
      },{
        "level": "INF",
        "model": "C"
      },{
        "level": "INC",
        "model": "A"
      },{
        "level": "IND",
        "model": "A"
      },{
        "level": "IND",
        "model": "B"
      }
];

function processArray(input) {
    var buf = {}, result = [];
    input.forEach(function(item) {
        var model = item.model;
        var existing = buf[item.level];
        if (existing) {
           existing.model += '-' + item.model;         
        } else {
            existing = buf[item.level] = {
                level: item.level,
                model: model
            };
            result.push(existing);
        }
    });
    return result;
}

var processed = processArray(arr);
console.log(processed);

Comments

0

function processArray(arr) {
    var result = [];
    arr
        .map(function(v) {return v.level;})
        .forEach(function(v, i) {
            var resultValue = result.filter(function(el, j) {
                return el.level === v;
            })[0];

            if(!resultValue) {
              result.push(arr[i]); 
            } else {
              resultValue.model += '-' + arr[i].model;
            }
        });
    return result;
}

var arr = [
      {
        "level": "INF",
        "model": "A"
      },{
        "level": "INF",
        "model": "B"
      },{
        "level": "INF",
        "model": "C"
      },{
        "level": "INC",
        "model": "A"
      },{
        "level": "IND",
        "model": "A"
      },{
        "level": "IND",
        "model": "B"
      }
];

alert(JSON.stringify(processArray(arr), null, 2));

Comments

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.