# How to input a same property into an array

**URL:** <https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791>\
**Category:** General\
**Created:** [27 February 2023 07:32 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791 "2023-02-27T07:32:33Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![cameo69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cameo69/32/66593_2.png) [@cameo69](https://discourse.nodered.org/u/cameo69)\
**Post date:** [23 March 2023 15:04 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/21 "2023-03-23T15:04:50Z")

</div>

> [@Buckskin](#):
>
> or checkout [javascript - Changing the order of the Object keys.... - Stack Overflow](https://stackoverflow.com/questions/6959817/changing-the-order-of-the-object-keys)

That stack overflow thread is very informative.

If I understand correctly since ES6 there is an order ([The traversal order of object properties in ES6](https://2ality.com/2015/10/property-traversal-order-es6.html)) and on the stack overflow are numerous ways to re-sort the keys.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [23 March 2023 15:21 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/22 "2023-03-23T15:21:02Z")

</div>

So JavaScript continues to "improve" itself to the point of no return 🙂.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [24 March 2023 19:47 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/23 "2023-03-24T19:47:02Z")

</div>

I used the Stack Overflow replies to come up with this;

```auto
    / **************************************************************************************************
    *
    * Description. Create a new Object, by adding the properties of the old Object in order of the Array of 'keys'
    * If required, add any remaining properties of old Object to new Object
    *
    * @param {Object} object The object to operate on
    * @param {Array} keysInOrder An Array of the keys required, in order to be added
    * @param {boolean} addAll True if all properties of old Object to be added to new Object, false if only keys. Default is true
    * in Array to be added
    *
    * @return {Object} An Object with the properties in 'key' order of the provided Array, plus any other properties in
    * original order
    *
    * @Example let orderedObject = orderObjectProperties({c: 1, a: 2, b: 1}, ['a', 'b'], false) becomes {a: 2, b: 1}
    */

    function orderObjectProperties(object, keysInOrder, addAll = true) {
        let newObject = {}
        // Add the new Object properties in order of 'keys' in Array
        for (const key of keysInOrder) {
            if (Object.keys(object).includes(key) && !Object.keys(newObject).includes(key))
                newObject[key] = object[key]

        }

        // Add any old Object properties not yet added, if required
        if (addAll) {
            for (const key of Object.keys(object)) {
                if (!Object.keys(newObject).includes(key))
                    newObject[key] = object[key]

            }

        }

         return newObject

    } // End Function orderObjectProperties()

```

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [25 March 2023 00:57 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/24 "2023-03-25T00:57:02Z")

</div>

Perhaps you should add it to the flows, so its easier to find.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 May 2023 00:57 UTC](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791/25 "2023-05-24T00:57:20Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/how-to-input-a-same-property-into-an-array/75791.md?page=1)
