Ways to populate the second nested array with new values without overwriting existing ones

I am encountering the following issue:

this.logs = {};

this.logs[1] = resp;

In my initial array, I have the result shown in the image below: https://i.sstatic.net/RScSm.png

However, when I try to add a second level array with another array like this:

this.logs[1][i] = resp;

The values of the second array overwrite the strings of the first array. I am unsure of how to proceed in this scenario. My goal is to have a second level array without overwriting the strings of the first array. The outcome I want exists in the image referenced here: https://i.sstatic.net/7ItLG.png

If anyone has any suggestions on how to achieve this, please let me know as I am stuck on what steps to take next.

Answer №1

It's still a bit unclear to me what your goal is, particularly with the double ngFor. Here is an example of how you can merge values from two arrays into one:

function mergeArrays() {
  let results = [];

  const arrayOne = ['red', 'green', 'blue', 'yellow', 'orange'];
  const arrayTwo = ['apple', 'banana', 'cherry', 'date', 'fig'];

  results = arrayOne.map((value, index) => { return value + ' ' + arrayTwo[index]});

  console.log('Merged array result:')
  console.log('results:', results)

  // Merged array result:
  // results: Array(5) [ "red apple", "green banana", "blue cherry", "yellow date", "orange fig" ]
}

Check out the demo: https://jsfiddle.net/abc123/

I hope I grasped your intention correctly; if not, could you please clarify what you mean by a second level?

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

Altering the input type of a cloned element in Internet Explorer results in the loss of the value

When I have checkbox inputs displayed in a modal with preset value attributes, upon clicking "OK", I clone them and change their input types to hidden, then append them to a div in the document body. However, when trying to retrieve their values using jQue ...

Creating dynamic Ionic slides by fetching data from a database

I am currently experimenting with Ionic. Web development is not my strong suit, so I may be a bit off the mark. However, I would like to retrieve data from an SQLite database and display it on an ion-slide-box. Here is what I have attempted: function sel ...

Anonymized Score Reporting through Google Analytics analytics.js

Recently, I've been working on setting up Google Analytics for tracking my web game. Everything seems to be working fine, except for one issue - when I check my custom report the next day after testing the game, I notice that all the scores have been ...

Receiving a 200 response code, however the controller's store() method is not being accessed

Recently, I made the decision to switch from using a form in a blade file to a Vue-based form that utilizes Axios for posting data. After making these changes, I encountered an issue where I receive a 200 response, but my controller's store() method i ...

AngularJS ng-submit can be configured to trigger two separate actions

I am currently diving into AngularJS and working on a simple project to create an expense tracker as a beginner task. However, I have encountered some issues with my code. While I have successfully implemented most functions, I am struggling with the upda ...

Challenges with jQuery Image Sliders

Currently, I am learning from a tutorial on creating a custom jQuery content slider and you can find the tutorial here. Everything is working smoothly in terms of creating the image slider. However, I am facing an issue where I want to have the 'Next ...

Tips for implementing an `onclick` event on this specific button

I've utilized JavaScript to generate a button. Now, I'm wondering how to incorporate an onclick event for this button. var b1 = document.createElement("button"); b1.setAttribute("class", "btn btn-default"); b1.setAttribute("id", "viewdetails"); ...

Nodejs image validation not working as expected

I'm currently working on a project using Nodejs and the expressjs framework. My goal is to allow image uploads through an API, but I have two specific requirements: 1) I need to implement validation for the dimensions (height and width) of the image. ...

Dynamically switch between doubling Ajax calls in JavaScript

Encountering an issue with a jQuery click function. Triggering the click event on an HTML button dynamically loads styled checkbox and toggle switches based on their stored on/off state in a database. An additional click function is added to each loaded t ...

The TouchableOpacity function is triggered every time I press on the Textinput

Every time I press on a text input field, the login function is called This is the touchable statement: <TouchableOpacity style={InputFieldStyle.submitButton} onPress={this.login(this.state.email, this.state.password)}> <Text ...

Harnessing the power of the map function in TypeScript

Here is a collection of objects: let pages = [{'Home': ['example 1', 'example 2', 'example 3']}, {'Services': ['example 1', 'example 2', 'example 3']}, {'Technologies&apos ...

A step-by-step guide on extracting the image source from a targeted element

Here is a snippet of my HTML code containing multiple items: <ul class="catalog"> <li class="catalog_item catalog_item--default-view"> <div class="catalog_item_image"> <img src="/img/01.png" alt="model01" class="catalog_it ...

Unusual actions exhibited by a combination of JavaScript functions

Encountering an issue with a JavaScript file containing multiple functions causing strange behavior. The Logging.js file is responsible for writing to a text file: function WriteLog(message) { var fso = new ActiveXObject("Scripting.FileSystemObject") ...

Experiencing difficulties posting on route due to receiving an undefined object instead of the expected callback in Node Js

I am working on implementing a feature in my application where users can create a favorite route. When a user adds a campground to their favorites, the ID of the campground is saved in an array within the schema. The process involves checking if the campgr ...

Parcel js is encountering difficulties running the same project on Ubuntu

I have encountered an issue with my JavaScript project when trying to run it on Ubuntu using parcel 2 bundler. The code works perfectly fine on Windows, but in Ubuntu, I am facing errors. Despite trying various solutions like cleaning the cache and reinsta ...

Querying MongoDB to filter data using multiple conditions simultaneously

How can MongoDB be used to filter documents based on multiple fields and value types? In my dataset, I have documents with different fields that I want to use as filters to retrieve specific documents. For example: The Person field can have values like ...

Ways to display additional text with a "Read More" link after three lines of content without relying on a

I am currently working on an application where I need to display text in a limited space of 3 lines. If the text exceeds this limit, I want to show either "Read More" or "Hide". Below is the code snippet that I am using for this functionality. class Cust ...

Issue with Vue method not providing expected output

As I dive into the world of Vue, I find myself facing a peculiar issue with a method that should return a string to be displayed within a <span>. While I can successfully retrieve the correct value through console.log, it seems to evade passing into ...

Having trouble with your Jquery resize or scroll function?

function adjustNavbar() { if ($(window).width() > 639) { $(document).scroll(function () { if ($(window).scrollTop() > 60) { $('.navbar').addClass('background', 250); } else { ...

How to remove the initial negative value present in an array using C#

I have searched for various solutions, but none of them effectively address my requirement to eliminate the initial negative number in a sequence while retaining the others (there may be several negative numbers in the array). ...