0

I have a json data like this:

var data = [{
    "RM_Name": "Cloud Strife",
    "Division": "Avalanche",
    "RM_Phone": "No Phone",
    "RC_Name": "No RM",
    "RC_Phone": "No Phone",
    "States_Covered": "Midgar,Nibelheim"
}, {
    "RM_Name": "Clint Eastwood",
    "Division": "Hollywood",
    "RM_Phone": "No Phone",
    "RC_Name": "No RC",
    "RC_Phone": "No Phone",
    "States_Covered": "Gran Torino"
}, {
    "RM_Name": "Barret Wallace",
    "Division": "Avalanche",
    "RM_Phone": "No Phone",
    "RC_Name": "Barry Allen",
    "RC_Phone": "No Phone",
    "States_Covered": "North Corel"
}, {
    "RM_Name": "Tifa Lockheart",
    "Division": "Avalanche",
    "RM_Phone": "No Phone",
    "RC_Name": "Aeris Gainsborough",
    "RC_Phone": "No Phone",
    "States_Covered": "Sector 7"
}];

Now I want to create a new array consist of RM_Name and RC_Name like this so the output would be like this:

[Cloud Strife,Clint Eastwood,Barret Wallace,Barry Allen,Tifa Lockheart,Aeris Gainsborough]

How can I do that? I have tried using $.merge(). But I think the output is wrong. Any help is greatly appreciated.

2
  • Your example does not include the No RM and No RC entries, should these be ignored? Commented May 6, 2014 at 9:43
  • @RoryMcCrossan yes just ignore it i just need to see how to combine both properties :) Commented May 6, 2014 at 9:45

6 Answers 6

3

Use jQuery .each() to iterate JSON. Try this:

var dataArray = [];
$.each(data, function(i, v){
       dataArray.push(v.RM_Name);
       if(v.RC_Name !="No RC")
       dataArray.push(v.RC_Name);
});
console.log(dataArray);

DEMO FIDDLE

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

Comments

2

Demo Fiddle
jQuery

var toShow = new Array();
$.each(data,function(i,item){
    toShow.push(item.RM_Name);
    if(item.RC_Name !="No RC")
        toShow.push(item.RC_Name);
});  

It would even ignore each "RC_Name":"No RC"

Hope it helps...

Comments

2

Doing it without jQuery and ignoring No RC and No RM.

Demo Fiddle

var result = [];
for(var i = 0, l = data.length; i < l; ++i)
{
    if(data[i].RM_Name !== 'No RM')
       result.push(data[i].RM_Name);
    if(data[i].RC_Name !== 'No RC')
       result.push(data[i].RC_Name);
}
console.log(result);

What this does is:

  • loop over the data array
  • if the RC_Name is different than 'No RC' push it in the array
  • if the RM_name is different than 'No RM' push it in the array
  • output the array

Comments

2

Functional options (No Jquery) ~ Both IE9+ only without poly-fill.

var ar = data.reduce(function(previous, current)
{ 
    previous.push(current.RM_Name, current.RC_Name);  
    return previous;
}, []);

or (although not overly readable)

var ar = Array.prototype.concat.apply([], data.map(function(row){ 
    return [row.RM_Name, row.RC_Name]; 
}));

3 Comments

reduce is kinda of new, not supported in older browsers without polyfill. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@JoshMc Same refers to Array.prototype.map as well.
Get so used to including a polyfill, you forget which ones are for what browsers ^^.
1

Use .each() in jquery

var name = [];
$.each(data, function( index, value ) {
        name.push(value.RM_Name);
        if(value.RC_Name !="No RC")
        name.push(value.RC_Name);  
});

console.log(name);

Comments

0

Another possible solution with $.map and $.grep:

[].concat.apply($.map(data, function(o) {
    return $.grep([o.RM_Name, o.RC_Name], function(v) {
        return v !== 'No RM' && v !== 'No RC';
    });
}));

You may omit the $.grep part if you don't need to filter properties by value:

[].concat.apply($.map(data, function(o) {
    return [o.RM_Name, o.RC_Name];
}));

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.