0

I have the following JavaScript object:

{
    date  : 'some date',
    name  : 'some name'
}

I have 10 of these objects, half will have one value for name - tableA. And the other half will have a different value for name - tableB.

I want to break these objects down into different tables depending on the value of name, I won't know the name value of my object.

So for the 5 objects in the tableA table:

tableA
---------------
date  : 'some date'
date  : 'some date'
date  : 'some date'
date  : 'some date'
date  : 'some date'

And tableB

tableB
---------------
date  : 'some date'
date  : 'some date'
date  : 'some date'
date  : 'some date'
date  : 'some date'

I'll use jQuery to render all this. I'm assuming I'll need something like

  jQuery.each(objects, function(index, object) {
      //build my table
  }

I'm just not sure of the logic needed to accomplish what I want. Not the actual code to build my HTML, but the logic required to break my object into different 'groups'. I hope that makes sense.

Any advice or suggestions would be greatly appreciated.

4 Answers 4

1
var data = [{"date":"some date", "name":"tableA"},
            {"date":"some date", "name":"tableB"},
            {"date":"some date", "name":"tableA"},
            {"date":"some data", "name":"tableB"},
            {"date":"some data", "name":"tableC"},
            {"date":"some data", "name":"tableC"},
            {"date":"some data", "name":"Whatever"}];

var table = {};
$.each(data,function(index_data,obj_data){
    if(typeof table[obj_data.name] == 'undefined'){//there's the name in obj already?
        table[obj_data.name] = [{"date":obj_data.date,"name":obj_data.name}];//if no,then create an array and add the obj 
    }else{
        table[obj_data.name].push({"date":obj_data.date,"name":obj_data.name});//if yes add the obj to array
    }
});

here's the sample jsfiddle

it will create the property of table object that is name's property of dataobject and add the array of objects that has name's property of data object is the same of name's property of table object

feel free to ask me if you don't understand :)

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

Comments

0

You can use brilliant function of JavaScript - filter.

For example let's imagine you have array of objects:

var data = [{"date":"date1", "name":"tableA"},
            {"date":"date2", "name":"tableB"},
            {"date":"date3", "name":"tableA"},
            {"date":"date4", "name":"tableB"}];

You can describe two functions. Like:

function all_A(el) {
    if(el.name == 'tableA') {
        return true;
    }
}

function all_B(el) {
    if(el.name == 'tableB') {
        return true;
    }
}

Function all_A will return true for each element if name attribute of it element is "TableA". Something similar for all_B.

Then you need just run filter function on your data array. Like that:

var table_A = data.filter(all_A);
var table_b = data.filter(all_B);

Here is working example :) - http://jsfiddle.net/32a97n1b/

2 Comments

Thanks a lot KryDos, but I won't actually know the name value of object. I didn't state that in my original question. I've updated my question now.
What do you mean? you have two arrays. You can go though these arrays in the $.each loop and take "date" attribute from each object in these arrays
0

Try using following code. It will give you keys in json object.

for (data in objArray){
  console.log(data);
}

Further you can access individual element depending on your key and filter them

Comments

0

So you want to group the data into separate lists by the "name" column:

function group(column){
    return function(p,c){
        if (!p[c[column]]){
            p[c[column]] = [];
        }
        p[c[column]].push(c);
        return p
    }
}

data.reduce(group("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.