logo
down
shadow

Arrange Array by Given another array property as order in javascript


Arrange Array by Given another array property as order in javascript

By : Harsat
Date : November 18 2020, 03:01 PM
fixed the issue. Will look into that further I have here 2 array's , You can sort the main function based on the order_basis array.
code :
function getFormatted(main, order_basis){
  order_basis = order_basis.map(x => x.tag);
  return main.sort(function(a, b){
    if(order_basis.indexOf(a.tag) > order_basis.indexOf(b.tag))
      return 1;
    return -1;
  });
}

var order_basis = [{ tag: 'vip' }, { tag: 'home' }, { tag: 'work' }],
main_data = [{ tag: 'work', name: 'sample', contact: '0987654', email: 'sample@email.com' }, { tag: 'home', name: 'sample', contact: '0987654', email: 'sample@email.com' }, { tag: 'home', name: 'sample', contact: '0987654', email: 'sample@email.com' }, { tag: 'work', name: 'sample', contact: '0987654', email: 'sample@email.com' }, { tag: 'vip', name: 'sample', contact: '0987654', email: 'sample@email.com' }];


console.log(getFormatted(main_data, order_basis));
.as-console-wrapper { max-height: 100% !important; top: 0; }


Share : facebook icon twitter icon
Arrange objects by property with specific order using javascript

Arrange objects by property with specific order using javascript


By : Rizling
Date : March 29 2020, 07:55 AM
I wish this help you Is there a way that I could arrange an array of objects according to a certain property whose order is based on an external array (not just alphabetically, numerically, etc.)? , something like this:
code :
var sorted = objectArray.sort(function(a, b){
return tempArray.indexOf(a.section)-tempArray.indexOf(b.section)
});
Arrange and array in a order in php?

Arrange and array in a order in php?


By : H.Lyons
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I have a table where multiple images are uploaded .. the fields in the table are , select an array with position as keys of array for example..
code :
$image[position] = image_name;
$image[1] = image1;
$image[3] = $image3;
$image[5] = $image5;
for($i=0;$i<8;$i++){
    if(!isset($image[$i])) $image[$i] = '';
}
Arrange array of Data into Matrix Order in Javascript

Arrange array of Data into Matrix Order in Javascript


By : Long Bunly
Date : March 29 2020, 07:55 AM
I wish this helpful for you Am not able find logic, Here I need arrange the below data into multidimensional with matrix format, , You could sort the array in advance and then group the objects.
code :
var data = [{ matrixData: [8, 0], x: 8, y: 0 }, { matrixData: [0, 4], x: 0, y: 4 }, { matrixData: [4, 0], x: 4, y: 0 }, { matrixData: [0, 0], x: 0, y: 0 }, { matrixData: [4, 4], x: 4, y: 4 }],
    groups = data
        .sort(({ matrixData: a }, { matrixData: b }) => a[1] - b[1] || a[0] - b[0])
        .reduce((r, o, i, a) => {
            if (!i || a[i - 1].matrixData[1] !== o.matrixData[1]) {
                r.push([o]);
            } else{
                r[r.length - 1].push(o);
            }        
            return r;
        }, [])
        
console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }
How do I re-arrange a char array according to another sequence (which is a float array sorted in ascending order)?

How do I re-arrange a char array according to another sequence (which is a float array sorted in ascending order)?


By : viet hung
Date : March 29 2020, 07:55 AM
wish of those help For an assignment, I've been tasked with creating a Polyclinic Patient Attendance viewer. I have been given the following data: Percentage attendance for 4 different cases for a given year. For the project, we have to complete 5 functions. I've got 4 down but I still can't get my head around 1. According to the question, I have to display "The lowest to the highest percentage of case and name of the case for the year". I understand bubble sorting and I am able to arrange the percentages in ascending order. What I struggle to do however is display the names of the cases according to that ascending order I can get. , To swap strings you need strcpy() (prototype in )
code :
            //swap[1][28] = caseName[d][28];
            //caseName[d][28] = caseName[d + 1][28];
            //caseName[d + 1][28] = swap[1][28];
            strcpy(swap[0], caseName[d]);
            strcpy(caseName[d], caseName[d + 1]);
            strcpy(caseName[d + 1], swap[0]);
int indexes[] = { 0, 1, 2, 3 };
// sort indexes <== YOUR TASK
// indexes now is { 1, 0, 2, 3 }
for (i = 0; i < 4; i++) {
    printf("%f for %s\n", year2010Cases[indexes[i]], caseName[indexes[i]]);
}
Javascript array arrange parent and child as array?

Javascript array arrange parent and child as array?


By : user3011918
Date : March 29 2020, 07:55 AM
seems to work fine how can I create a Parent with Nested child as array format? The child will depend to the parent id and they will be groups as an array under "squads" key? , My working solution:
code :
let final = [];
let sample = [
  { tribe_id: 1, tribe_name: "Tribe A", squad_id: 1, squad_name: "Squad A" },
  { tribe_id: 1, tribe_name: "Tribe A", squad_id: 2, squad_name: "Squad B" },
  { tribe_id: 2, tribe_name: "Tribe B", squad_id: 3, squad_name: "Squad C" }
];
console.log(sample);

const uniqueTribes = sample.reduce((acc, current) => {
  const x = acc.find(item => item.tribe_id === current.tribe_id);
  if (!x) {
    // return acc.concat([current]);
    return acc.concat([{ "tribe_id": current.tribe_id, "tribe_name": current.tribe_name, "squads": [] }]);
  }
  return acc;
}, []);
// console.log(uniqueTribes);

for (let i = 0; i < uniqueTribes.length; i++) {

  let uniqueTribeName = uniqueTribes[i].tribe_name;
  let uniqueTribeId = uniqueTribes[i].tribe_id;

  for (let x = 0; x < sample.length; x++) {
    let squadName = sample[x].squad_name;
    let squadId = sample[x].squad_id;

    if (uniqueTribeId == sample[x].tribe_id) {
      // uniqueTribes[i].squads.push({ squad_id: sample[x].squad_id, squad_name: sample[x].squad_name });
      uniqueTribes[i].squads.push({ squad_id: squadId, squad_name: squadName });

    }
  }
  // let uniqueTribeId = sample[i].squad_name;
}
console.log(uniqueTribes);



let tribe = [];

// console.log(tribe);

let desiredFormat = [
  {
    tribe_id: 1, tribe_name: "Tribe A", squads: [
      {squad_id: 1, squad_name: "Squad A"},
      {squad_id: 2, squad_name: "Squad B"}
    ]
  },
  { 
    tribe_id: 2, tribe_name: "Tribe B", squads: [
      {squad_id: 3, squad_name: "Squad C"}
    ]
  }
];
Related Posts Related Posts :
  • Chrome-Extension/JavaScript How to make all links on page have no effect?
  • Websocket issue Internet Explorer
  • sessionStorage value becomes null in other tab in AngularJS
  • Split URL from position x to the end in Javascript
  • How to set value of input field with NgIf in Angular 4
  • invalidStateError for second time screenshare on Chrome
  • How do you pass a value to the variable in a xhr.setRequestHeader?
  • I cant start nodemon on my windows 10
  • Nested if else jumps out from the main if else in javascript
  • Iterate through all collections and remove them
  • ES6 - Value of a variable in template string does not increment
  • how to get content of html table cell using javascript javascript depending on checkbox
  • Node.js Working with Multiple Tier SQL
  • Can't allow Cross-Origin Request in local Nodejs server
  • Datatables orderable=false directly on table
  • Dynamically load js content with iron router in meteor using array in return statement
  • Limit checkbox to 2, but moving the select instead of blocking it
  • Why itsn't displaying marker on map?
  • jQuery target next of the same class in the whole DOM
  • encoding must be a valid encoding error in process encoding
  • How to do a date range to view data from selected date highcharts
  • How do I refactor this composed function with Ramda.js?
  • jQuery to serialize selected option's value, not label
  • Convert object to Chartis.js structure
  • Dynamic import with not bundled file
  • Click handler not firing for elements rendered in Ajax Callback - only in Safari
  • Math.max.apply gives an error CreateListFromArrayLike called on non-object
  • get the parent nodes to a given node in d3 dendogram
  • is-dropdown-submenu-parent makes links unclickable on Opera and Chrome
  • JS: Sum column values and updating result on change
  • Query against nested object keys
  • Fullcalendar getting its parent colspan
  • How to retrieve a dynamic number of elements by id
  • How to access redux variables and functions from deep components
  • Efficiently access Array.prorotype.filter single-item returns
  • PHP var into Javascript
  • How to parse json with javascript/jquery if content matches string
  • How can I make a pyramid based on alphabet?
  • Javascript sorting a csv rows and displaying in table
  • How to get specific data from json from api using ajax. The json looks strange to me
  • react js html5 video not working
  • Javascript Angular 4 Change ngClass from another Component
  • Hide multiple table columns with javascript
  • Convert datetime to ISO Format using moment js
  • Dynamically adjust canvas size depending on content
  • setTimeout function with document on click
  • Need advice on nested Promises.all
  • Google Captcha expiration time
  • Syntax of Javascript TAPE unit test
  • Parse php json to javascript
  • Write to Internet Explorer javascript console with vba in excel
  • 2 Forms with Jquery
  • Why the loop stops right in the middle?
  • filter system for restaurant booking
  • how to add active class in framework nav menu when (add/edit) pages are opened
  • Port a js map in dart
  • How to get value from select using javascript
  • Audio won't be muted with JavaScript - removing audio tags with mutationobserver needed
  • How to include an HTML/Javascript game into reactJS website
  • Convert Nodejs server into node module
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org