Creating an array of input data for component, implementing data formatting in TypeScript for Angular or React applications

Here is an example of the input for the component. When this input is passed to the component, the output displays correctly.

 const data = [
            ['Rank', 'Player', 'Player', 'Player'],
            ['1', 'sachin', 'dravid', 'ganguly'],
            ['2', 'pointhig', 'mcrath', 'warne'],
            ['3', 'sanath', 'murali', 'vaas'],
            ['4', 'akram', 'younis', 'akhamed'] 
        ]  

However, my API response looks like the following. I attempted to convert the API response into the input data format shown above, with the Rank column automatically incrementing. Unfortunately, my conversion attempts were unsuccessful.

0:Array(3)
    0:{fullname:'sachin'}
    1:{fullname:'dravid'}
    2:{fullname:'ganguly'}
1:Array(3)
    0:{fullname:'pointhig'}
    1:{fullname:'mcrath'}
    2:{fullname:'warne'}
2:Array(3)
    0:{fullname:'sanath'}
    1:{fullname:'murali'}
    2:{fullname:'vaas'}
3:Array(3)
    0:{fullname:'akram'}
    1:{fullname:'younis'}
    2:{fullname:'akhamed'}

Answer №1

const teams = ["Team 1", "Team 2", "Team 3", "Team 4"];
const players = [
  [{ name: "John" }, { name: "Sarah" }, { name: "Mark" }],
  [{ name: "Emily" }, { name: "David" }, { name: "Michael" }],
  [{ name: "Rachel" }, { name: "Peter" }, { name: "Olivia" }],
  [{ name: "Sam" }, { name: "Lily" }, { name: "Tom" }]
];

const lineup = players.map((item, idx) => [
  (idx + 1).toString(),
  ...item.map(x => x.name)
]);
const teamLineup = [...[teams], ...lineup];

console.log(teamLineup);

Answer №2

One way to achieve this is by utilizing the nested map() method.

const arrayOfArrays = [[
    {fullname:'sachin'},
    {fullname:'dravid'},
    {fullname:'ganguly'}
],
    [{fullname:'pointhig'},
    {fullname:'mcrath'},
    {fullname:'warne'}],
[{fullname:'sanath'},
{fullname:'murali'},
{fullname:'vaas'}],

[{fullname:'akram'},
{fullname:'younis'},
{fullname:'akhamed'}]]

const result = arrayOfArrays.map((array, index) => [index + 1 ,...array.map(item => item.fullname)]);
console.log(result)

Answer №3

Provided below is a response showcasing varied header sizes

const list = [
  [{
    title: 'apple'
  }, {
    title: 'banana'
  }, {
    title: 'orange'
  }],
  [{
    title: 'blueberry'
  }, {
    title: 'strawberry'
  }, {
    title: 'kiwi'
  }],
  [{
    title: 'grape'
  }, {
    title: 'pear'
  }, {
    title: 'plum'
  }],
];

const output = list.map((item, index) => [
  index + 1,
  ...item.map(subItem => subItem.title),
]);

output.unshift([
  'Index',
  ...[...Array(output[0].length - 1)].map(_ => 'Fruit'),
])

console.log(output);

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

Tips for implementing multiple nested routes using Vue.js?

Can Nested Routes be created with more than 2 levels? I am looking to implement a structure like this: +--------------------+ | User | | +----------------+ | | | Profile | | | | +------------+ | | | | | About | | | | | | +------ ...

The NextJS App directory consistently stores the retrieved data in the cache, regardless of any no-cache options that may have been configured

Despite trying various online advice, I am still facing the issue of cached fetched data. I am using a mongodb server, and even though I add data to the server, I can only see the new data on the server itself, not on the website (before the NextJS project ...

Leveraging Arrays with AJAX Promises

I am currently working on making multiple AJAX calls using promises. I want to combine the two responses, analyze them collectively, and then generate a final response. Here is my current approach: var responseData = []; for (var i=0; i<letsSayTwo; i++ ...

What is the best way to initiate a function from within another function while working with ReactJS?

Seeking guidance on how to trigger a Redux state change by calling a function from another function using the onClick event. Currently, I am able to invoke the Jammingmenu upon clicking an icon, however, no action is performed or alert displayed. Any assis ...

Ways to insert space between tags in jade compiled through webpack

My Angular template is structured like so: ul li(ng-repeat-start="item in items") {{item.name}} br(ng-repeat-end) Can I add a space or newline character between li and br? Currently, it looks like this: { test: /\.jade$/, loader: 'raw!jade ...

Replacing a div with another div can be achieved in a single swap

My goal is to swap one div with another div upon clicking using the provided code. It functions properly for a single instance. However, the issue arises when the class appears multiple times, causing all instances to change due to sharing the same class n ...

Changing the value within a deeply nested object

I am facing an issue with a nested object in my code: { id: "id", name: "Name", type: "SC", allgemein: { charname: "Name", spieler: "Jon", }, eigenschaften: { lebenspunkte: "30", }, talente: {}, zauber ...

Troubarked by problems NodeJS faces when trying to establish a connection with CosmosDB using a connection

Having trouble with my code that fails when I try to create a new instance of the CosmosClient. The option to create a CosmosClient using a connection string should be straightforward. The environment variable holds the necessary connection string in this ...

What is the best way to vertically flip a background image that is repeating along the y axis in CSS?

I am in the process of developing a mobile web app and I need assistance with flipping the background image when it repeats along the y-axis. Can someone guide me on how to achieve this using CSS or JavaScript? https://i.stack.imgur.com/b107V.jpg var el ...

What are some ways to implement AJAX with input from the user?

I'm currently working on a project to create a basic web page that will make use of AJAX for displaying results. Within main.py, I have a dictionary of words: words = { 'a': True, 'aah': True, 'aahed': True, ...

Building a table using a JSON object in a React component

I have been dynamically creating a table in React based on the API response received. data = {"name":"tom", "age":23, "group":null, "phone":xxx} Everything was working fine until I encountered a scenario w ...

Transform an array into an object with assigned key names

How can we transform the following array: [2019,2020,2021] into the format: { 0: {year:2019}, 1: {year:2020}, 2: {year:2021} } ...

Node.js app hosted on Azure Functions unable to resolve NPM dependencies

I set up a Node (TypeScript) Azure Function application using the VSCode Azure Function extension. While checking the deployment output, I noticed the following log line: Started postDeployTask "npm install (functions)". Despite this, I couldn&a ...

Anticipated an array but received something else (JSON) - AngularJS Error

Currently, I am immersing myself in Angular1 with the help of Adam Freeman's "Pro AngularJS" book. However, I have encountered a hurdle while trying to build a DeployD app as outlined in chapters 6-8. It appears that my code is having trouble reading ...

Having trouble getting the video to load on your Mozilla Firefox browser?

Unable to play video on Firefox. Error message: URL.createObjectURL(video): Error decoding media resource blob: NS_ERROR_DOM_MEDIA_FATAL_ERR (0x806e0005) Details: Decoder may not support the requested video format with YUV444 chroma subsampling. Tried usi ...

Method with undefined constructor parameter in Typescript

Understanding how to effectively use Typescript and Angularjs in conjunction has been a challenge for me. Despite reading numerous blog posts and consulting the official documentation for both projects, the concepts just haven't fully clicked. My Ang ...

Can 2D canvas elements make use of CSS shaders?

Can CSS shaders, like "fragment" shaders, be utilized in 2D canvas contexts as well? ...

AngularJS Error: $element.find(...) does not support iteration

I'm attempting to utilize a datatable in my AngularJS application. Below is the HTML code I've implemented: <div ng-app="datatable"> <div ng-controller="voucherlistcontroller"> ...

Leveraging Jquery to add an element or text in front of the initial instance of a different element

Here is a sample of my xml code: <entry> <br> <abc></abc> <xyz></xyz> <example></example> <example></example> <example></example> </entry> <entry> <br> <abc>&l ...

Utilizing jQuery for real-time operations

I am working with two input text fields and I am looking to utilize jQuery to perform an operation between them. Here are the input text fields: <p> <label>Title 1</label> <span class="field"> <input type="text" ...