0

I have an element in my record json file as following:

"custitem_list": [
    {
        "internalid": "1",
        "name": "FLAT AND DULL"
    },
    {
        "internalid": "2",
        "name": "FRIZZY"
    }
] 

Now I need to push the list names from the record into another json record as option values in attribute element.Following is the structure that I'm after.

"attributes": [
    {
        "id": 0,
        "name": "Custlist",
        "position": 0,
        "visible": true,
        "variation": false,
        "options": [
            {
                "FLAT AND DULL",
                "FRIZZY"
            }
        ]
    }
]

I have tried the following ways, but none of then is working as expected:

{
    "id": 0,
    "name": "Custlist",
    "position": 0,
    "visible": true,
    "variation": false,
    "options": [
        {
    {{#each record.custitem_list}}
    {{#if @index}}
    "{{{name}}}"
    ,{{/if}}
    {{/each}}
    }
    ]
}

{
    "id": 0,
    "name": "Custlist",
    "position": 0,
    "visible": true,
    "variation": false,
    "options": [
        {
            {{#each record.custitem_list}}
            {{#if @index}},{{/if}}
            {"name": "{{{name}}}"}
            {{/each}}
    ]
}

{
    "id": 0,
    "name": "Custlist",
    "position": 0,
    "visible": true,
    "variation": false,
    "options": [
        {
           {{#each record.custitem_list }}
           {{#if @index}},
           {{/if}}
           "{{{name}}}"
           {{/each}}
        }
    ]
}

Could anyone shed me some light?

Many many thanks

Seppe Mariën
  • 355
  • 1
  • 13
user843812
  • 91
  • 1
  • 5
  • 1
    Please try to give good valid code without errors next time and format it so that it easier to read. – Seppe Mariën May 21 '21 at 23:57
  • 1
    Does this answer your question? [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – Seppe Mariën May 22 '21 at 00:00

1 Answers1

0

Try this:

const names = obj.custitem_list.map(item => item.name);
obj2.attributes.options = names
Seppe Mariën
  • 355
  • 1
  • 13