handlebars - How to merge all elements from one array into another array as the value of a single element

In my JSON record file, there is an element called "custitem_list" with the following structure:

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

My goal now is to extract the list names from this record and push them into another JSON record as option values in an attribute element. Here is the desired structure:

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

I have attempted multiple methods to achieve this, but none of them are giving the expected results. Can someone provide me with some guidance on this matter?

Thank you very much.

Answer №1

Here's a suggestion:

Consider using the following code snippet:
<script>
let allNames = items.list.map(eachItem => eachItem.name);
newObject.attributes.options = allNames;
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

NextJS application failing to display SVG icon in the absence of internet connection

https://i.stack.imgur.com/M9reE.jpg https://i.stack.imgur.com/Yyg4g.jpg Upon inspection of the provided images, it is evident that the src URL points to a location within the nextjs public folder. The issue arises when there is no internet connection - i ...

It appears that Vivus JS is having difficulty animating specific <path> elements

I've been experimenting with the Vivus JS library, which is fantastic for animating paths such as drawing an image. However, I'm facing an issue where my SVG icon should animate to a 100% line-width, but it's not working as expected. Interes ...

Add to the current values of the REACT Form template property

I am new to working with REACT and I have been exploring whether it is possible to append a REACT Form control property value in order to enhance its functionality. To streamline the validation process, I have created a validation template that leverages ...

Challenge with Vue fetching data

I need to make an update request using fetch. Here is the code snippet: methods: { updateData() { fetch("http://localhost:3000/selectedData", { method: "PATCH", headers: { "Content-Type": & ...

invoking a function by utilizing nested controllers

Struggling with nested controllers, the main goal of this code is to link data extracted from a .json file to another function or file. .html file: <div ng-app="myApp" ng-controller="GetCtrl" > <li ng-controller="ChannelCtrl" ng-repeat="x in ...

Is there a way to retrieve values from two input fields using the onclick function?

In a div, I have added two input fields and an update button. Here's the code structure: <button type = "button" id = "add-botton" >Add Element </button> <div id = "trace-div1" class = "trace"> <h4><span>Trace 1& ...

Tips for parsing information from a text file and displaying it on a website in table format

Would like to create a 2x4 table where the first column has a dropdown for selection. Upon choosing an option, the associated data will populate the other 3 columns. I'm new to this, so please bear with me. Using x, y, z as placeholders, the data wil ...

What is the method for incorporating a timeout in a promise?

After exploring various methods for adding timeouts to promises, it appears that most rely on the setTimeout() function. Here is the formal definition: The setTimeout() function executes a specified function or evaluates an expression after a set number of ...

Unable to close window with window.close() method after initially opening it with JS or JQuery

I am currently using an Instagram API that requires users to log out through the link . This link redirects users to the Instagram page, but I want them to be redirected to my own page instead. Although I tried different methods from a previous post on thi ...

Troubleshooting an issue with importing a Component in ReactJS using material-ui

Using the material-ui library, I attempted to create a Table following the code provided in the Custom Table Pagination Action example. However, I encountered the following error: Error Encountered: Warning: React.createElement: type is invalid -- expect ...

Display the initial JSON data object without the need to choose a link with the help of AngularJS

I have successfully built a webpage that displays JSON data based on the selected link. However, I am facing an issue where I need to show the first JSON data object before selecting any link (initially). Check out the Plunker demo here: http://embed.plnk ...

How to handle duplicate keys in JSON parsing using Python3

Excuse my lack of experience in this area, but there's a json response that looks like this; import json jsonObj = json.loads(""" { "data": [ { "name_space": "name", "value": &q ...

Is it possible to use a shell script to replace the external CSS file link in an HTML file with the actual content of the CSS file

Seeking a solution for replacing external CSS and JS file links in an HTML document with the actual content of these files. The current structure of the HTML file is as follows: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C ...

Automatically rehydrate an instance using Angular and JavaScript

REVISION Special thanks to Shaun Scovill for providing an elegant solution using lodash // Creating an instance and injecting server object - within the ChartService implementation below var chart = new Chart(serverChartObject); // Replacing ...

Type-safe Immutable.js Records with TypeScript

I'm struggling to find a suitable solution for my query. I am aiming to define data types using an interface in TypeScript, but my data consists of Immutable.js records making it more complex. Please refer to the example provided below. interface tre ...

Can you send an array of objects as data in an AJAX post request?

My primary objective is to gather the dropdown values from a webpage and then send them to a PHP file for processing. Currently, I am using jQuery to achieve this by creating an overall schedule array and adding each element to it for updating. Here' ...

Ways to consistently stream information from server to browser

I have a file that is constantly being updated with the following contents: id,value,location 1234,pass,/temp/... 234,fail,/temp/r/... 2343,pass,/temp/status/... The file keeps updating for approximately 1 hour by a program. I want to display ...

Calculating the Exact Number of Days Using Moment.js, Node npm, and Handlebars

Incorporating moment.js into node Handlebars has been beneficial. I came across this helpful resource: . While using it, I encountered an issue where I needed to calculate the number of days between the present day and a future date, and then display this ...

Express handlebars does not support client-side javascript

This is a unique template <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>My Amazing Website</title> ...

Closing the dropdown menu by directly clicking on the button

I've noticed several inquiries on this platform regarding how to close a drop-down menu by clicking anywhere outside of it. However, my question is a bit different. I want the dropdown-menu to remain open once clicked, only closing when the user clic ...