458

The code below comes from jQuery UI Autocomplete:

var projects = [
    {
        value: "jquery",
        label: "jQuery",
        desc: "the write less, do more, JavaScript library",
        icon: "jquery_32x32.png"
    },
    {
        value: "jquery-ui",
        label: "jQuery UI",
        desc: "the official user interface library for jQuery",
        icon: "jqueryui_32x32.png"
    },
    {
        value: "sizzlejs",
        label: "Sizzle JS",
        desc: "a pure-JavaScript CSS selector engine",
        icon: "sizzlejs_32x32.png"
    }
];

For example, I want to change the desc value of jquery-ui. How can I do that?

Additionally, is there a faster way to get the data? I mean give the object a name to fetch its data, just like the object inside an array? So it would be something like jquery-ui.jquery-ui.desc = ....

4
  • You would have to transform the array into a Javascript object in order to use the syntax projects["jquery-ui"].desc. Would that be worth the effort only to get nicer syntax? Commented Jan 14, 2011 at 10:26
  • I've updated my solution with your latest question. And you can use the "projects.jquery-ui.desc" notation. Commented Jan 14, 2011 at 10:28
  • ** ↑ aston means you can use that notation if the object structure is changed as per his answer below. (Not with the OP's existing example structure.) Commented Oct 27, 2020 at 22:04
  • For new people, just use .find() it's a method for the array and is very useful in this case. See abe kur's answer on this. Commented Mar 2, 2021 at 3:22

34 Answers 34

1
2
-1

A short one liner

Also Safe

(projects.find(pr => pr.value === 'jquery-ui') || {}).desc = 'changed'
Sign up to request clarification or add additional context in comments.

Comments

-1

//lets assume toupdateObjectId == 1

const updatedArray =  myArray.map(arr => {
  if (arr.id === toupdateObjectId) {
    return { ...arr, desc: newDesc };
  }
  return arr;
});
console.log(updatedArray)

Comments

-2

This is my response to the problem. My underscore version was 1.7 hence I could not use .findIndex.

So I manually got the index of item and replaced it. Here is the code for the same.

 var students = [ 
{id:1,fName:"Ajay", lName:"Singh", age:20, sex:"M" },
{id:2,fName:"Raj", lName:"Sharma", age:21, sex:"M" },
{id:3,fName:"Amar", lName:"Verma", age:22, sex:"M" },
{id:4,fName:"Shiv", lName:"Singh", age:22, sex:"M" }
               ]

Below method will replace the student with id:4 with more attributes in the object

function updateStudent(id) {
 var indexOfRequiredStudent = -1;
    _.each(students,function(student,index) {                    
      if(student.id === id) {                        
           indexOfRequiredStudent = index; return;      
      }});
 students[indexOfRequiredStudent] = _.extend(students[indexOfRequiredStudent],{class:"First Year",branch:"CSE"});           

}

With underscore 1.8 it will be simplified as we have methods _.findIndexOf.

Comments

-2

Let you want to update value of array[2] = "data"

    for(i=0;i<array.length;i++){
      if(i == 2){
         array[i] = "data";
        }
    }

2 Comments

Is this an answer or a question?
You successfully updated the 2nd item in that array in the most complicated way possible.
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.