Converting an Object's value into an array and appending a new value

Is there a way to convert an Object value to an array and add additional values to this new array? Let's consider the following scenario:

const object = {
 names: "John"
}

How can the 'names' property be converted from a string to an array similar to this?

const namesFromApi = ['Ken', 'Ben', 'Joana', 'Fillip']

namesFromApi.map(el => {
object.names = [names, el]
})

In addition, how can a new name be added to the newly created array using a string value?

Answer №1

Is this what you're referring to? Incorporating the spread operator and square brackets to combine the names

const person = {
 fullName: "Alice"
}
const additionalNames = ['Karen', 'Brett', 'Sarah', 'Megan']

person.fullName = [person.fullName,...additionalNames]

console.log(person)

Answer №2

To generate a fresh array, you can follow these steps:

const data = {
    title: "Coding"
}
const newArray = [data.title];

const titlesFromServer = ['Emma', 'Liam', 'Alice', 'Oliver'];
titlesFromServer.forEach(item => {
newArray.push(item);
});

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

Enforcing automatic spell check on dynamically generated content

After some experimentation, I have discovered that the spell check feature in most browsers is only activated when the user moves to the next word after inputting text. Another interesting finding is that it is possible to "query" the spellchecker by simpl ...

The response was rejected by the newspaper, as per UVA 11340 regulations

I recently tackled problem 11340 on OnlineJudge.org (), where the task involved determining the cost of an article based on specified prices per character. My solution passed the provided tests as well as an additional test on . However, despite working c ...

Guide on accessing js file in an Angular application

I have a component where I need to create a function that can search for a specific string value in the provided JavaScript file. How can I achieve this? The file path is '../../../assets/beacons.js' (relative to my component) and it's named ...

What is the solution for the red underline issue with the @json Blade directive in VSC?

Everything seems to be functioning correctly with these variables, but I'm curious as to why they are underlined in red. Does anyone have a solution for this issue? https://i.sstatic.net/mzlkY.png Error message https://i.sstatic.net/4LajF.png ...

Data not being returned by AJAX request

I'm currently working on implementing a script in the background of my PHP pages to periodically check for new messages in the database and notify users accordingly. To achieve this, I decided to utilize AJAX to make a call to a file containing the n ...

Experiencing problems with lining up several circular items in a row

For my project, I am trying to showcase multiple circular shapes with percentages in a row, but I am encountering some issues. I attempted using a div with display flex, but it doesn't seem to be working as expected. Here is what I have tried so far: ...

Utilize regular expressions to substitute a specific string of text with an HTML tag

My task is to transform this text: let text = "#Jim, Start editing to see some magic happen!"; into this format: text = "<span style="color:red">#Jim</span>, Start editing to see some magic happen!"; Here is my ...

Unforeseen issue within Vuejs-nuxt (SSR Mode) is preventing the retrieval of the UserUUID through the getter in plugins, resulting in an unexpected '

In my vuejs-nuxt project using SSR mode, I encountered an issue when trying to call a socket event. I need to pass the userID to the socket from the store. The GetUserUUID getter functions properly in all other APIs except when called from the "plugin/sock ...

Learn how to display months on a countdown timer and then customize the format with basic JavaScript for a website

Looking to create a countdown page for the upcoming ICC cricket world cup event that displays the remaining days in two different formats: Format #1: 01 months 10 days 10 hours Format 2: 01 hours 20 minutes 10 seconds (If less than 2 days remain) I curr ...

Sharing server object in expressJS with another file through module.exports

As I work on my expressJS app, I encountered a situation where I needed to share the server object with another file. To achieve this, I decided to create the server in my app.js file and then expose it to one of my routes. var server = http.createServer( ...

Implementing Keycloak Policies to Secure a Node.js API

Hello everyone, this is my first time reaching out for help here so please bear with me if I miss out on any important information or make some mistakes. Apologies in advance for the lengthy text. Summary of My Objective (I might have misunderstood some ...

What is the most strategic way to conceal this overlay element?

Currently, the website I'm developing features a series of navigation elements at the top such as "Products" and "Company." Upon hovering over the Products link, an overlay displays a list of products with clickable links. Positioned at the top of the ...

What is the process to subscribe and obtain data from a server-to-user channel using pusher-js?

I am currently hosting my application using next.js on Vercel. I want to integrate Pusher to provide real-time messages to users in a private and secure manner. Despite successful log entries, I am facing challenges in subscribing to the channel and retrie ...

Generate numerous div elements by utilizing ng-repeat

I am trying to generate 'n' red blocks with text (where n represents the number of elements in an array), but unfortunately, I am seeing a blank page. <html> <body> <script src="https://ajax.googleapis.com/ajax/libs/angu ...

Interactive Autocomplete Component

I am encountering issues with passing dynamic data to my autocomplete angularjs directive, which is built using jQuery-UI autocomplete. Below is the current code I am working with: HTML: <div ng-app="peopleApp"> <div ng-controller="indexCont ...

Troubleshooting the "missing checks.state argument" error in AUTH0 debugging

I recently launched my new NextJs blog application at on Vercel, but I'm encountering some issues with getting auth0 to function properly. Upon clicking the login button located in the top right corner of the screen, users are redirected to auth0 to ...

Allow users to select options after they click a radio button

I have a pair of radio buttons and two sets of select options within different classes. Upon selecting the first radio button, the select options belonging to class1 should be enabled. On the other hand, upon selecting the second radio button, the select ...

Significant slowdown observed when deleting multiple objects from the Three.js scene

We are facing a challenge when dealing with large Three.js scenes that consist of many individual objects. In some cases, our scenes can contain anywhere from 25,000 to 50,000 instances of Object3D. While this may seem like a lot, we haven't found an ...

Guide on adding multiple items to an array using the $push method in Mongoose

I'm encountering some difficulties when trying to push multiple objects into an array. Despite experimenting with various variations of the code, I've found that I either end up directly pushing the objects to the database or only pushing the la ...

Utilizing a JavaScript Library in your Scala.js Project: A Step-by-Step Guide

I am currently following a tutorial on setting up dependencies in my Scala.js project. Here is the link to the tutorial: First and foremost, I have organized my project setup as shown below: https://github.com/scala-js/scalajs-cross-compile-example Wi ...