1

I have an array and object which look like this:

const myArray = [];

_meta: {
    pageContext: {
        pageType: "Homepage",
        label: "pageContext"
    }
}

and I want to push the object pageContext including it's key into an array. How do I do that? If i do:

myArray.push(model._meta.pageContext);

I get only the contents of page context in my array. I want the result to be like:

[
    pageContext: {
        pageType: "Homepage",
        label: "pageContext"
    }
]

Is there fancypants es6 trickery to do this?

3
  • Are you not getting any syntax errors? Especially for the first example...? Commented May 15, 2017 at 12:55
  • w3schools.com/jsref/jsref_push.asp have u seen this ? Commented May 15, 2017 at 12:56
  • You could just assign directly to the "key" you want... myArray.pageContext = model._meta.pageContext Commented May 15, 2017 at 12:58

4 Answers 4

2

I want the result to be like

What you've shown after that is invalid (well, okay, you could create something like that, but it's generally a bad idea*). You can create this, though:

[
    {
        pageContext: {
            pageType: "Homepage",
            label: "pageContext"
        }
    }
]

Note that the array contains an object, which contains pageContext.

If that's the result you want, and if _meta is really as shown, just push _meta:

myArray.push(model._meta);

Example:

const myArray = [];

const model = {
  _meta: {
      pageContext: {
          pageType: "Homepage",
          label: "pageContext"
      }
  }
};

myArray.push(model._meta);
console.log(myArray);

If _meta has other things in it that you don't want to push, create a new object:

myArray.push({pageContext: model._meta.pageContext});

Example:

const myArray = [];

const model = {
  _meta: {
      pageContext: {
          pageType: "Homepage",
          label: "pageContext"
      },
      somethingElse: {
           weDontWant: "this"
      }
  }
};

myArray.push({pageContext: model._meta.pageContext});
console.log(myArray);


* If you really want to have an array with a pageContext property on it, you can do that, because arrays are objects:

myArray.pageContext = model._meta.pageContext;

But most of the time, if you think you want to do that, you don't want an array at all, you just want a non-array object.

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

Comments

1

An array can contain objects and not keys, Your expected output is an invalid object

Your array can look like

[{
    pageContext: {
        pageType: "Homepage",
        label: "pageContext"
    }
}]

by doing

myArray.push(model._meta);

1 Comment

yes, that's what i'm looking for. Unfortunately there's a lot in meta I don't need but i'll just have to delete that after adding _meta object.
0

But what you want is invalid JS. You could obtain something like

[
    { 
        pageContext: {
            pageType: "Homepage",
            label: "pageContext"
        }
    }
]

by using

myArray.push(model._meta);

or something like

[
    { 
        name: 'pageContext',
        value: {
            pageType: "Homepage",
            label: "pageContext"
        }
    }
]

by using

myArray.push({ name: 'pageContext', value: model._meta.pageContext });

Comments

0
[
    {
        pageType: "Homepage",
        label: "pageContext"
    }
]

If you already know that each object is pageContext then push

myArray.push(model._meta.pageContext);

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.