4

I have several objects like this:

enter image description here

I want to move type and value one step up so they will be next to field, and then delete data.
It looks like this when departments is converted to JSON:

[
    {"field"    : "DEPARTMAN_NO",
     "data"     : { "type":"numeric" , "comparison":"eq" , "value":11 }
    },
    {"field"    : "DEPARTMAN_ADI",
     "data"     : { "type":"string" , "value":"bir" }
    }
]

I have tried:

departments = grid.filters.getFilterData();
i = {};
for(var i in department) {
department = i.data;
delete.department.data;
};

but it dosen't work.

4
  • Can you post markup/code? I can't see your picture on my intranet? Commented Jun 29, 2012 at 9:05
  • @ElRonnoco, I wonder what you use. Commented Jun 29, 2012 at 9:07
  • @ElRonnoco, I have updated for you. Commented Jun 29, 2012 at 9:23
  • The reason your delete doesn't work is because you need to say delete obj[property] where property is the property to delete. Also you don't declare i properly - you should remove i = {}. See my answer. Commented Jun 29, 2012 at 10:34

4 Answers 4

5

1) First, loop departments, each item we call it department;
2) You want to move department.data's properties to department, From another angle, you can move department's properties to department.data and return department.data, code like:

var departments = [{
        "field": "DEPARTMAN_NO",
        "data": {
            "type": "numeric",
            "comparison": "eq",
            "value": 11
        }
    }, {
        "field": "DEPARTMAN_ADI",
        "data": {
            "type": "string",
            "value": "bir"
        }
    }],
    department;

for (var i = 0, len = departments.length; i < len; i++) {
    department = departments[i]; // department
    for (var key in department) {
        if (key !== 'data' && department.data) {
            department.data[key] = department[key];
        }
    }
    departments[i] = department.data || department; // if no department.data, no change
}

console.log(departments);

result:

enter image description here

view the full demo http://jsfiddle.net/KVYE5/

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

3 Comments

i was going to answer just the way up here. but wiky faster so +1 :)
However there are several obj s within departments
On the demo code, obj is an item of departments, and you just need to loop the 'departments', others are same.
2

I wrote a little npm package that does what you're asking for: moving a property up a level in an object.

You can get it here: https://www.npmjs.com/package/move-property-up-a-level

Usage

var movePropertyUpALevel = require('movePropertyUpALevel');

var fakeObj = {
    poodle: {
        first: {
            hey: 'you'
        },
        second: 'meAgain'
    }
};

movePropertyUpALevel(fakeObj, 'poodle');

console.log(fakeObj.first.hey);
//'you'
console.log(fakeObj.poodle);
//undefined

1 Comment

"Useage" is misspelled.
1
obj =
[
    {"field"    : "DEPARTMAN_NO",
     "data"     : { "type":"numeric" , "comparison":"eq" , "value":11 }
    },
    {"field"    : "DEPARTMAN_ADI",
     "data"     : { "type":"string" , "value":"bir" }
    }
];


for ( var item in obj ) {
    if ( obj[item].field && obj[item].data ) { //check the 'field' and 'data' exist
        obj[item].field = {
            dept : obj[item].field ,         //department name is put into a property
            type : obj[item].data.type,      //so is data.type and data.value..
            value: obj[item].data.value      //..all are now contained in 'field'
        };
        delete obj[item].data;               //remove the 'data' object
    }
}

console.log(obj);

Comments

0
department.type = department.data.type;
department.value = department.data.value;
delete department['data'];

5 Comments

I think I have to loop because it says that department.data.type is undefined.
Is departement.data defined? Can you give us a greater view of your object tree ?
I think the Java Script engine doesn't understand what object I'm referring to because there are 2 objects within departement. So it might be more appropriate to write something like departement.[0].data.
Maybe. That's a valid syntax. It's hard to say if it's appropriate without knowing the whole object tree.
departement.data is not defined. I have updated the question.

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.