Combine arrays using ES5 syntax. Join arrays to form a single array

I need help merging chunks of arrays into a single array of objects using AngularJS.

My initial input array looks like this:

[
  [
    {
      "title": "asd",
      "description": "asd"
    },
    {
      "title": "asz",
      "description": "sd"
    }
  ],
  [
    {
      "title": "ws",
      "description": "sd"
    },
    {
      "title": "re",
      "description": "sd"
    }
  ],
  [
    {
      "title": "32",
      "description": "xxs"
    },
    {
      "title": "xxc",
      "description": "11"
    }
  ]
]

The above input needs to be transformed into an array of objects as shown below:

[
  {
    "title": "asd",
    "description": "asd"
  },
  {
    "title": "asz",
    "description": "sd"
  },
  {
    "title": "ws",
    "description": "sd"
  },
  {
    "title": "re",
    "description": "sd"
  },
  {
    "title": "32",
    "description": "xxs"
  },
  {
    "title": "xxc",
    "description": "11"
  }
]

I've achieved this in ES6 as follows:

const input=[[{"title":"asd","description":"asd"},{"title":"asz","description":"sd"}],[{"title":"ws","description":"sd"},{"title":"re","description":"sd"}],[{"title":"32","description":"xxs"},{"title":"xxc","description":"11"}]]
const output = [].concat(...input);
console.log(output);

However, I believe this is specifically for ES6. Can someone assist me with implementing this in ES5?

Thank you in advance

Answer №1

To combine multiple arrays within an input array, you can use the concat method:

var input = [
  [
    {
      "title": "apple",
      "description": "juicy fruit"
    },
    {
      "title": "banana",
      "description": "yellow fruit"
    }
  ],
  [
    {
      "title": "orange",
      "description": "citrus fruit"
    },
    {
      "title": "grape",
      "description": "small fruit"
    }
  ],
  [
    {
      "title": "watermelon",
      "description": "refreshing fruit"
    },
    {
      "title": "strawberry",
      "description": "sweet fruit"
    }
  ]
];

var result = Array.prototype.concat.apply([], input);
console.log(result);

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

What steps can you take to resolve the "TypeError: Cannot read property 'id' of undefined" issue?

I have been developing an app that involves using databases to add items for users based on their user ID, which is their username. However, whenever I attempt to add an item, I encounter an error that I can't seem to troubleshoot. The error message r ...

Display the two values of an object pair using ng-repeat in AngularJS

I have an array of objects structured like this: myCtrl.siteNameLabels = myCtrl.actual.map(function (value, index) { return { actualSite: { actualSiteName : value, actualSiteData: myCtrl.tableData[index] }, ...

Discover a technique to mimic the src attribute in an HTML5 video tag just like YouTube does

When inspecting the element, it is possible to view the src tag and obtain the video link in the browser. How can we deceive users with a fake URL so that they are unaware of the actual location of the video? For instance, if we examine the src tag of the ...

Implementing image caching in tvOS with React-Native: A step-by-step guide

Looking to Implement Image Caching in tvOS using React Native I'm trying to figure out how to cache images that I download from the internet on my Apple TV. I've experimented with various libraries, but none seem to be compatible with tvOS. Any ...

Guide to building interactive dropdown menus in Angular 8

In my user interface, each row contains three dropdowns for level 1, level 2, and level 3, as well as an "Add Level" button. When the "Add Level" button is clicked, a new set of three dropdowns for level 1, level 2, and level 3 should be added dynamically ...

Tips on searching for an entry in a database with TypeScript union types when the type is either a string or an array of strings

When calling the sendEmail method, emails can be sent to either a single user or multiple users (with the variable type string | string[]). I'm trying to find a more efficient and cleaner way to distinguish between the two in order to search for them ...

Remove browser data in PhoneGap / Prevent PhoneGap from utilizing cookies

Currently, I am in the process of creating a login system for my mobile application. After logging in to my server, it sends back a JSESSIONID that I can utilize for further authentication. The issue I am encountering is that PhoneGap is automatically st ...

The email notification system seems to be malfunctioning

Trying to implement a contact form on my page where users can provide feedback via email. The issue I'm facing is that I am receiving the email, but the page is not refreshing and no alert box is appearing. Can someone help me solve this problem? Here ...

Angular-meteor tutorials have a method known as '/parties/insert' that is already clearly defined and explained

I am currently diving into meteor + angular and enjoying learning through ! As I was working on the 3-way data binding section, I created a folder named collections within the socially folder. In this folder, I made a file called parties.ts where I added ...

What is the best method to clear data in a field following a change in selection in another field?

Currently, I am exploring the functionality of ejTimePicker from the reference site: In my project, I have implemented two select boxes, named textbox1 and textbox2, which store selected times. My goal is to reset the time in textbox2 whenever a different ...

Combining nested objects into a single object

Can a nested array object be flattened into a single object? In my query, I aim to eliminate the source object and combine all properties into one object (see the desired output below). var result = [ {"_id":"12345", "_type":"feeds", "_s ...

Sails.js navigating through sessions

What is a rolling session in Sails.js? A rolling session is a session that expires after a set amount of time without user activity, except for websockets live updating data. If the user navigates to a different part of the site before the session expires, ...

Issue encountered while setting up Karma on Node - problem accessing current directory - macOS

Today has been a struggle as I try to tackle an issue that Google hasn't been able to help with. I'm following the AngularJS tutorial, but I can't seem to install Karma. Every attempt results in an error message saying "Error retrieving curr ...

The MUI next Tooltip fails to display upon hovering

While using Material-UIv1.0.0-beta.34 Tooltip with Checkbox and FormControlLabel, I noticed that the tooltip works as expected when hovering over the label in one case. However, when I tried creating a new component(custom) with FormControlLabel and Checkb ...

Retrieve file server domain using JavaScript or jQuery

I'm trying to extract the domain name without the "http(s)://www." from a file link. For example, if the script returns "example.com", I want it to parse through links like "http://www.example.com/file.exe" or "https://example.com/folder/file.txt#some ...

How to retrieve and modify JSON data in Node.js using Stream with both Get and Post methods

Exploring Node.js for the first time, I'm currently tackling a project where I aim to utilize Request for streaming data from one endpoint to another. The objective is to leverage Request for extracting and posting an altered JSON body through a pipe ...

Conceal navigation buttons on the first and last pages of the slider

I'm attempting to develop a slider that hides the "previous" button when it is on the first slide and hides the "next" button when it reaches the last slide. The slider will be created using a combination of PHP, forms, and JavaScript. Below is the H ...

What is the process of transferring data from one div to another div (table) using AngularJS?

In my quest to enhance my table with JSON data upon clicking the + icon, I am faced with two sections: Pick Stocks where stock names and prices (retrieved from data.json) need to be added to the table found in the Manage Portfolio section. First Section h ...

Array with multiple dimensions using commas as delimiters

My array (array[]) contains elements in the format below, separated by a comma: array[0] = abc, def, 123, ghi I want to transform this array into another multi-dimensional array (arrayTwo[]), structured like this: arrayTwo[0][0] = "abc" arrayTwo[0][1] = ...

Display a button only when hovering over it

Seeking assistance for a simple task that is eluding me at the moment. I am currently using scss and trying to make a button only appear when hovered over. The button is hidden in the code snippet below, nested within a block alongside some svgs. Any hel ...