Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
I have list in the array .,how do I show one of its element should come first based on condition in mapping?

Here is the object.

 var arr = {
    firstValue:"xyz",
    content:[
      {
        "value": "abc",
        "checked": true
      },
      {
        "value": "xyz",
        "checked": false
      },
      {
        "value": "lmn",
        "checked": true
      }
    ]
    }

In this firstValue is xyz so while mapping xyz should come first like this:

 var arr = {
firstValue:"xyz",
content:[
  {
    "value": "xyz",
    "checked": true
  },
  {
    "value": "abc",
    "checked": false
  },
  {
    "value": "lmn",
    "checked": true
  }
]
}

How to achieve this by using javascript,

Thanks.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const arr = {
  firstValue: "xyz",
  content: [
    {
      value: "abc",
      checked: true,
    },
    {
      value: "xyz",
      checked: false,
    },
    {
      value: "lmn",
      checked: true,
    },
  ],
};
const index = arr.content.findIndex((item) => item.value === arr.firstValue);
if (index !== -1 && index !== 0) {
  arr.content.unshift(...arr.content.splice(index, 1));
}
about 4 years ago · Juan Pablo Isaza Report

0

It looks like you just want to sort the matching object to the top.

You can either use Array#sort(), though this will mutate the original object, so if it's in state you'll want to clone before sorting.

var arr = { firstValue: 'xyz', content: [ { value: 'abc', checked: true, }, { value: 'xyz', checked: false, }, { value: 'lmn', checked: true, }, ], };

const sorted = [...arr.content].sort((a, b) => 
  (b.value === arr.firstValue) - (a.value === arr.firstValue));

console.log(sorted);

Otherwise you can find the index of the object using Array#findIndex() and then use Array#splice() and Array#unshift() to move it to the beginning of the array. Here declaring a utility function and returning a copy of the passed array to avoid mutation.

var arr = { firstValue: 'xyz', content: [ { value: 'abc', checked: true, }, { value: 'xyz', checked: false, }, { value: 'lmn', checked: true, }, ], };

const orderFirst = (a) => {
  const res = [...a];
  const i = a.findIndex((o) => o.value === arr.firstValue);
  
  if (i && i !== -1) {
    res.unshift(res.splice(i, 1));
  }

  return res;
};

const sorted = orderFirst(arr.content);

console.log(sorted);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!