2

I have an object and some of its elements are object too. How can I make my variable just one object?

I mean I have a variable that has elements:

  • name
  • type

type is an object that has field:

  • name

So I one that:

  • name
  • type.name (not as an object just another variable as like name)

PS: When I inspect my variable with Firebug:

name
    "fsfaf"

password
    "242342425"

name
    "XXX"

type
    Object { name="sds"}

name
    "sfs"

Let's assume that I hold it with a variable myVariable. I want to do something like that:

var newVariable = somefunction(myVariable);

so my newVariable should be like that:

name
    "fsfaf"

password
    "242342425"

name
    "XXX"

type.name
    "sds"

name
    "sfs"
2
  • Why do you want to do this? Btw, if you have type.name type is a still an object! Commented Oct 31, 2011 at 12:03
  • I want a new element instead of type, type.name. type.name is not an object(I tested with Firebug what I want to do, type.name doesn't seem as an object) Commented Oct 31, 2011 at 12:07

2 Answers 2

4

I edited my answer too. Here's a way to do what you want:

var myVar = 
{
    name: 'myName', 
    type: { name: 'type.name' }
}

function varToNewVar(myVar)
{
    var newVar = {};

    for(var i in myVar)
    {
        if(typeof myVar[i] === 'object')
        {
            for(var j in myVar[i])
            {
                newVar[i+'.'+j] = myVar[i][j];
            }
        }
        else
            newVar[i] = myVar[i];
    }

    return newVar;
}

var newVar = varToNewVar(myVar);

However, this way you cannot access newVar.type.name, you can access newVar['type.name'], which I guess is OK for what you want to accomplish...

P.S. Test extensively before applying to live projects, etc. I imagine a lot of things can go wrong, depending on the objects that you're using. There should be no problem to use it in the situation you want though

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

4 Comments

Applying to live projects? If kamaci has a project which requires such tricks then this project should never be deployed. :) This code is bad. I mean not from the coding point of view, but from the designing point of view.
@Nikoloff voting up for your helps, that's what I want. I don't want to use newVar.type.name, that's OK. Just one thing more. How can I do that within same object I mean without using newVar, just with myVar?
You can just use this method on the same variable, e.g.: myVar = varToNewVar(myVar);. This can be moved as a method within the object too, replacing the parameter with this. Can't give you an example right now, gotta go... Also, see what @freakish said :)
@freakish I am developing a Spring REST project. I have an object that has list type elements in it. Jackson marshals it into a JSON that can have JSON array type elements. I will use it in a grid, jqGrid that wants an array at my logic. That's why I am trying to do it.
-1
var o = {
  "name": "my_name",
  "type.name": "type's name"
}

Seriously though, what? What do you actually want?

function IAmAUselessHack(o) {
  o["type.name"] = o.type.name;
}

7 Comments

I have already an object that has object type elements in it. I want to transform it with a function. My input is always object that holds some objects.
what you mean with 'IAmAUselessHack'?
I will send that variable to a plugin that only accepts objects with fields(not have object type members) that's why I am trying to do it.
@kamaci I'm implying what your doing is a waste of time.
it is not a waste of time. I use a plugin that accepts element just array. I mean not holding array objects. On the other hand my source is object that can include object elements. So how can it be waste of time?
|

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.