Arranging objects in an array based on a separate array of strings

Here is an array of objects that I need to rearrange:

var items = [
  { key: 'address', value: '1234 Boxwood Lane' },
  { key: 'nameAndTitle', value: 'Jane Doe, Manager' },
  { key: 'contactEmail', value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9bf5f0f7f1fcf2f6ebf7dffaf0fcf4f1b3fef2f0">[email protected]</a>' },
  { key: 'contactPhone', value: '9876543210' },
  { key: 'localPhone', value: '9876543210' },
  { key: 'status', value: 'pending' },
]

I have another array called order with the desired order for these objects:

var order = [
  'nameAndTitle',
  'contactPhone',
  'contactEmail',
  'address',
]

I need to arrange the items array according to the order specified in the order array. Any ideas on how to achieve this?

Answer №1

To organize the indices, consider sorting them by their differences.

var objs = [{ key: 'address', value: '1234 street' }, { key: 'nameAndTitle', value: 'John Smith, CEO' }, { key: 'contactEmail', value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ae0e5e2e4f9e7e3fee2caede7ebe3e6a4e9e5e7">[email protected]</a>' }, { key: 'contactPhone', value: '1234567890' }],
    keys = ['nameAndTitle', 'contactPhone', 'contactEmail', 'address'];

objs.sort((a, b) => keys.indexOf(a.key) - keys.indexOf(b.key));

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

To have full control over the sequence, utilize an object with preset order values and include a default property with a lower value than others to sort unknown keys at the start or with a higher value for the end of the array.

This method also suits complex sorting criteria where unlisted items can be sorted in the middle if required.

var objs = [{ key: 'localPhone', value: '1234567890' }, { key: 'status', value: 'open' }, { key: 'address', value: '1234 street' }, { key: 'nameAndTitle', value: 'John Smith, CEO' }, { key: 'contactEmail', value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b414443455846425f436b4c464a424705484446">[email protected]</a>' }, { key: 'contactPhone', value: '1234567890' }],
    order = { nameAndTitle: 1, contactPhone: 2, contactEmail: 3, address: 4, default: Infinity };

objs.sort(({ key: a }, { key: b }) => (order[a] || order.default) - (order[b] || order.default));

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

ES5

var objs = [{ key: 'localPhone', value: '1234567890' }, { key: 'status', value: 'open' }, { key: 'address', value: '1234 street' }, { key: 'nameAndTitle', value: 'John Smith, CEO' }, { key: 'contactEmail', value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8a2a7a0a6bba5a1bca088afa5a9a1a4e6aba7a5">[email protected]</a>' }, { key: 'contactPhone', value: '1234567890' }],
    order = { nameAndTitle: 1, contactPhone: 2, contactEmail: 3, address: 4, default: Infinity };

objs.sort(function (a, b) {
   return (order[a.key] || order.default) - (order[b.key] || order.default);
});

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

Answer №2

To maintain the integrity of your original array, you have the option to create a new array by pushing the objects in the desired order.

However, if altering the original array is not a concern, you can simply utilize the sort function which will arrange the keys based on their position in the 'keys' array (there are resources available to assist you with this method).

edit @Nina Scholz quickly implemented the solution ^^

const objs = [{
    key: 'address',
    value: '1234 street'
  },
  {
    key: 'nameAndTitle',
    value: 'John Smith, CEO'
  },
  {
    key: 'contactEmail',
    value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e040106001d03071a062e09030f0702400d0103">[email protected]</a>'
  },
  {
    key: 'contactPhone',
    value: '1234567890'
  },
];

const keys = [
  'nameAndTitle',
  'contactPhone',
  'contactEmail',
  'address',
];

const array = keys.reduce((tmp, x) => {
  const item = objs.find(y => y.key === x);

  if (item) {
    tmp.push(item);
  }

  return tmp;
}, []);

console.log(array);

Answer №3

To rearrange the array, you can utilize the sort method like this:

var items = [{
    category: 'fruit',
    name: 'apple'
  },
  {
    category: 'vegetable',
    name: 'carrot'
  },
  {
    category: 'fruit',
    name: 'banana'
  },
  {
    category: 'vegetable',
    name: 'lettuce'
  }
]

var categories = [
  'vegetable',
  'fruit',
]

items.sort(function(a, b) {
  return categories.indexOf(a.category) - categories.indexOf(b.category);
});

console.log(items)

Answer №4

One way to achieve this is by following these steps:

Iterate through a string array and compare the value (nameAndTitle) with the corresponding index value in your object array. If there is no match, locate that string within the key-tag in your object array and relocate it to the correct index position. This approach should help you accomplish the task effectively.

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

Using JSON to insert an array into an object with identical key name

var arr = ['1', '2', '3'] var part = {} var partContents = [] arr.map(function(i){ partContents.push({ obj: i }) part['text'] = partContents }) console.log(part); Is there a way to create separate arrays with ...

How can I extract an object from an array by using a string key in either Observable or lodash?

How can I retrieve a specific object (show) from Shows based on its id being a string in a given sample? I am transforming the result into an RXJS Observable, so using functionalities from RXJS or lodash would be greatly appreciated. //JSON RETURNED with ...

What is the process for completing a form with Protractor and TextAngular?

I'm currently attempting to submit a form that utilizes TextAngular for certain input fields. Despite my efforts, I haven't been successful in finding information on how to fill out these fields. The closest solution I found was this, but when I ...

When using jQuery and AJAX together, it seems that the POST method is returning

Currently experimenting with Cloud9. The current project involves creating an image gallery. On the initial page: There are a few pictures representing various "categories". Clicking on one of these will take you to the next page showcasing albums fro ...

Using AngularJs's filter to display a specific string when the model evaluates to true

I'm currently exploring how to create a filter that can display a specific value if the model passed in via the scope is true. For instance, when my database returns true or false for: thing.hearted, I want a filter to output "hearted" only if thing. ...

Tips for receiving accurate HTML content in an Ajax request

I have used an Ajax call to fetch data from a function that returns an entire HTML table. $.ajax({ url: "/admin/project/getProjectTrackedTimes", headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('cont ...

In search of a straightforward Angular class directive that can easily add background color styling

When I was working on my modal screen, I noticed that the checkboxes displayed a green border due to the ng-model attribute. To fix this issue, I added a style with background-color: #FFFFFF; which successfully removed the green border. <div cl ...

Having trouble implementing object type switching in Typescript

While in the process of developing an angular application, I stumbled upon a peculiar issue. Some time ago, I crafted this piece of code which performed flawlessly: selectedGeoArea: any receiveStoreEvent(event) { switch (event.constructor) { ca ...

NodeJS File Upload: A Step-by-Step Guide

I need assistance with uploading an image using nodejs. I am able to successfully send the file to node, but I am unsure how to handle the "req" object. Client <html> <body> <input id="uploadInput" type="file"/> < ...

I'm encountering an issue with automatically updating content on a webpage

I am currently working on a webpage that refreshes its content automatically to display the most up-to-date data from the database. Here's the JavaScript code I'm using: setInterval(function() { $("#status").load('refresh.php'); }, ...

Activate video in Slick Slider with the click of a button

I am facing an issue with my slider setup, where each slide contains a video as the background along with play/pause buttons. When I try to play the video by clicking the corresponding button on a specific slide, I encounter this problem: if I click the pl ...

Using Cypress fixtures with TypeScript

After transitioning from using Cypress with Javascript specs to Typescript, I encountered a challenge in working with Fixtures. In Javascript, the approach below worked; however, I faced difficulties when switching to Typescript. Fixture JSON file: I sto ...

The access to Angular.js has been denied with a 403 Forbidden error

I'm seeking assistance with a specific issue I am facing. For an upcoming job interview, I have been tasked with cloning and understanding a project - Repository HUBTaxi-Server. I am struggling as this project is larger than what I am used to working ...

Display elements that are unique to one array and not found in another array using React

I am working on a feature where users can select fruits from an array called fruits, and the selected fruits will be stored in another state array named user_fruits. Once a fruit is selected by a user, it should not be available for selection again. fruit ...

Learn how to achieve a sleek animation similar to the famous "Ken Burns effect" by utilizing the CSS property "transform" instead of "object-position". Check out the demo to see it in action!

I am currently exploring how to create an animation similar to the "Ken Burns" effect using CSS transform properties. While I have been using object-position to animate, I am facing challenges with the fluidity of the movement. I am seeking help to achiev ...

Fixing the issue of scrollbars not working in existing divs while creating new jscrollpane divs in jQuery

Utilizing the jquery.uploadfile.min.js plugin to upload multiple files results in creating a div of jscrollpane class each time a file is uploaded. However, there seems to be an issue where only the scrollbars in the last created div are functioning proper ...

Using React-Router v6 to pass parameters with React

My App.js file contains all the Routes declarations: function App() { return ( <div className="App"> <Routes> <Route path="/"> <Route index element={<Homepage />} /> ...

The process of implementing server-side rendering for React Next applications with Material-ui using CSS

I have developed a basic React application using Next.js with an integrated express server: app.prepare() .then(() => { const server = express() server.get('/job/:id', (req, res) => { const actualPage = '/job' const ...

Angular schema forms allow users to make multiple selections at once through a

Having an issue with using a multiselect checkbox in a dropdown with Angular Schema Forms. My requirement is to disable another control based on a selected value, but I am unable to bind any events to the checking of a checkbox in the multiselect. Can so ...

Is there a way to retrieve the client's IP address from the server side within a Next.js application?

How can I determine the user's time zone and location in order to generate a server-side page tailored to their specific location and time zone? I am struggling to retrieve the user's IP address from the request or the localhost IP address (127.0 ...