Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

array object make api format javascript

const response = { users: { 144: [ { name: "Olivia", }, { mode: "c7", fkProductsIds: [ 3, 2 ] } ], 168: [ { name: "Jill", }, { mode: "p9", fkProductsIds: [ 1, 4, 5, 3 ] } ], 202: [ { name: "David", }, { mode: "p9", fkProductsIds: [ 5, 1, 2 ] } ] }, products: [ { 1: "PSM" }, { 2: "FP" }, { 3: "F2" }, { 4: "Mark4" }, { 5: "Astrid" }, ] }

getProductById = (id) => {
  var elem = response.products.find(elem => elem[id]);
  return {id : id, name: elem[id]}
}

var data = Object.keys(response.users)
        .map( userId => 
          ({
            id: userId, 
            name: response.users[userId][0].name, 
            mode: response.users[userId][1].mode, 
            products: response.users[userId][1].fkProductsIds.sort().map(getProductById)
        })).sort((a, b) => b.products.length - a.products.length)

console.log(data);
 Run code snippetHide results
Comment

PREVIOUS NEXT
Code Example
Javascript :: js react change slide by touch event 
Javascript :: javascript stopped working 
Javascript :: how to end tsc main --watch 
Javascript :: mongoose connecting directly rather than tunnel 
Javascript :: react native asyncstorage mergeItem example 
Javascript :: service erstellen angular 
Javascript :: React Tools - Suspense 
Javascript :: load mulitple elements in route v6 
Javascript :: loop with multiple conditions js codesmith 
Javascript :: javascript const scope = await angular.element(document.body).scope(); 
Javascript :: acceder a variable css desde js 
Javascript :: content editable remove style 
Javascript :: jquery datatable dropdown from stored procedure values 
Javascript :: strapi extend user model 
Javascript :: jquery crud table example 
Javascript :: typeorm caching queries time limit globally 
Javascript :: react get padding 
Javascript :: variable hoisting 
Javascript :: angular switch case multiple values 
Javascript :: Example of Logical OR assignment operator in es12 
Javascript :: show route between markers google maps javascript 
Javascript :: Subtracting Numbers in Array 
Javascript :: expo google sign inredirect uri mismatch 
Javascript :: get src vanilla js 
Javascript :: date javascript only show day month year 
Javascript :: x is not a function javascript type error 
Javascript :: rest framework and json 
Javascript :: avascript-how-to-detect-if-a-word-is-highlighted 
Javascript :: Refresh Mathjax formater 
Javascript :: javascripts 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =