Tips for combining the values of two objects that share the same keys

Received response:

data [4] { {
_id:"1" description:"desc" languageId:"5" title:"title1" } { [2]:
_id:"2" description:"desc1" languageId:"4" title:"title2" } { [3]:
_id:"3" description:"desc3" languageId:"4" title:"title3" } [4]:
_id:"4" description:"desc4" languageId:"4" title:"title4" } }

Now, I need to separate it like this:

 {
    [0]:Object
    description:"desc"
    title:"ttitle"
    description1:"desc1"
    title1:"title2"
    }
    {
    [1]:Object
    description:"desc3"
    title:"ttitle"
    description1:"desc4"
    title1:"title2"
    }

This task involves AngularJS. Any assistance would be greatly appreciated. I have attempted using push and merge methods. Thank you in advance for your help.

Answer №1

I wasn't entirely sure about the specifics of your query. Does this address what you were looking for?

http://plnkr.co/edit/yWc36FmiBs6gMaXBq9on?p=preview

function separate(data) {
  var results = [];

  for (var i = 0; i < data.length; i += 2) {
    var j = i + 1;

    var result = {
      description: data[i].description,
      description1: data[j].description,
      title: data[i].title,
      title1: data[j].title,
    };

    results.push(result);
  }

  return results;
}

Sample input:

  var info = [{
    _id: "1",
    description: "desc",
    languageId: "5",
    title: "ttitle"
  }, {
    _id: "2",
    description: "desc1",
    languageId: "4",
    title: "title2"
  }, {
    _id: "3",
    description: "desc3",
    languageId: "4",
    title: "title2"
  }, {
    _id: "4",
    description: "desc4",
    languageId: "4",
    title: "title2"
  }];

Sample output:

[{
  "description": "desc",
  "description1": "desc1",
  "title": "ttitle",
  "title1": "title2"
}, {
  "description": "desc3",
  "description1": "desc4",
  "title": "title2",
  "title1": "title2"
}]

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

Different ways to maintain the original syntax highlighting colors in JavaScript console

Upon closer inspection near the green arrows, you can see that the default console.log function colorizes values based on their type, distinguishing between string and number. https://i.sstatic.net/MtO8l.png In contrast, highlighted near the red arrows i ...

Anomalous Snackbar and Alert Interactions

Why am I getting an error with this code snippet: import Snackbar from "@mui/material/Snackbar"; import MuiAlert from "@mui/material/Alert"; const Alert = ({children}) => <MuiAlert>{children}</MuiAlert> <Snackbar ope ...

Enhancing Angular Material md-data-table with custom calculated columns and sorting using md-order-by

My column is a calculated column using {{desserts.value1/desserts.value2}} <md-table-container> <table md-table> <thead md-head md-order="myOrder"> <th md-column md-order-by="value1">Dessert (100g serving)</th> ...

retrieving data from the database table

I'm having trouble retrieving specific values from a table. No matter what input I provide, I always get results from both rows. I want to match the input ID with the ID in the table. The code I'm attempting to use can be found in the attached im ...

Tips for customizing the appearance of the active item in the navigation bar

Currently, I am developing a project with React.js using HTML, CSS, and JavaScript. My goal is to enhance the visual appeal of the active navigation item in the navbar by adding a white border around it. This way, the border will remain visible while the c ...

Is there DnD support in Chrome?

Looking to create a unique shopping cart similar to the one featured on netTutsPlus, but encountered compatibility issues with Chrome and Opera when implementing the drag and drop feature. In search of a solution that will work seamlessly across both brow ...

Updating MongoDB collections in Mongoose with varying numbers of fields: A step-by-step guide

Updating a mongodb collection can be challenging when you don't know which fields will be updated. For instance, if a user updates different pieces of information on various pages, the fields being updated may vary each time. Here is my current appro ...

Express JS causing NodeJS error | "Issue with setting headers: Unable to set headers after they have been sent to the client"

As I embark on my journey to learn the fundamentals of API development, I am following a tutorial on YouTube by Ania Kubow. The tutorial utilizes three JavaScript libraries: ExpressJS, Cheerio, and Axios. While I have been able to grasp the concepts being ...

Damaged .xlsx document originating from a Flask web application and an ajax POST submission

I've encountered an issue with my Flask web app where a URL route receives a post request containing JSON, converts it to an .xlsx file, and returns it using send_file(). On the server side, the generated .xlsx file appears to be correct. However, wh ...

Creating an interactive TABLE with twitching animations in a straightforward HTML and JavaScript project

Currently, I am tackling a JavaScript challenge involving placing eight queens on a chessboard in such a way that they do not attack each other. However, there seems to be an issue - whenever I place a queen in an unoccupied column, that particular column ...

Despite calling Bootstrap Switch JS, no action is being executed

Struggling with integrating Bootstrap Switch into my project that already uses Bootstrap and jQuery. It seems like the JavaScript isn't affecting the HTML, which is driving me crazy. I've tried adding Bootstrap Switch using both the gem and stra ...

Altering the content of an item while accessing it from an array

I am facing an issue where I need to update text elements on a webpage based on data retrieved from the server. Even though I can successfully store the references and values, I encounter an error when attempting to access the text value of the object. The ...

Retrieve a targeted table from a webpage through Ajax refresh

On a webpage, I have a table that has two different views: simple and collapsible. I want to be able to toggle between these views using a button click without the need to refresh the entire page. Below is the code snippet I am currently using: $(&apo ...

Utilizing a JPA Entity within a Jax-RS method that processes application/json data

For my application, I need to perform database actions using JPA based on requests to a JaxRS service. Below is the entity class used for JPA: import javax.annotation.Generated; import javax.persistence.*; import java.util.Date; @Entity public class St ...

Is there a way to trigger a function in Javascript without manual intervention, similar to how

I've recently embarked on a journey to unravel the mysteries of jQuery. Consider this scenario where you want an action to be triggered when an anchor tag is clicked. Let's analyze the code snippet below: $('.selector').click The symb ...

I am looking to display an image as a value in the dropdown menu of my Contact Form 7 on my WordPress website

I am currently working on a Wordpress website where I have a page showcasing 50 different company logos. Below the logo section, there is a form created using Contact Form 7. The logo section was built using a combination of HTML and CSS. Within the form ...

Is there a way to segregate method calls when there are two distinct paths that lead to the same destination?

Currently, I am in the process of creating an express app that consists of a login page, a signup page, and a display page. Both the signup and login pages redirect to the display page where only the username is displayed. However, I am interested in findi ...

Jade: utilizing a Jade block within a Mixin by passing it as a string

I have come across a Jade template that includes a call to a mixin at a specific point. One of the parameters passed to this mixin is a lengthy HTML string which the mixin then prints using != The code looks like this: +createHTML({firstSection:'< ...

Populate the present time according to the country chosen from the HTML dropdown menu

I need to populate a date-time field with the current date and time of a specific country when the user selects that country from an HTML drop-down. Can you please provide guidance on the different ways this can be achieved? Thank you in advance. ...

Custom pagination for next/previous links in Django REST framework

When it comes to backend operations, I have integrated the PageNumberPagination as the DEFAULT_PAGINATION_CLASS. Since I am utilizing vue.js along with fetch, there is no need for me to include the entire URL structure provided by django-rest-framework: " ...