简体   繁体   English

如何在javascript中更新对象的属性?

[英]How do you update the properties of an object in javascript?

How do you update the properties of an object in javascript? 如何在javascript中更新对象的属性?
I would to update "id" of 68917 distance from 8.8 to 4.5... I have access to lodash.js 我将68917距离的“ id”从8.8更新为4.5 ...我可以访问lodash.js

[
      {
        "_id":68917,
        "Distance":8.81,
        "Lat":"42.65322175",
        "Lon":"-73.77398886"
      },
      {
        "_id":131277,
        "Distance":"9.86",
        "Lat":"42.654658",
        "Lon":"-73.805866"
      },
      {
        "_id":62450,
        "Distance":6.58,
        "Lat":"42.67457566",
        "Lon":"-73.74902171"
      }]
your_object[0].Distance = 4.5;

如果您事先不知道索引:

_.findWhere(your_object, {'_id': 68917}).Distance = 4.5;

The code you have provided provides an array. 您提供的代码提供了一个数组。 If this is is representative of your code, you can then iterate over the objects in the array using a for loop: 如果这代表您的代码,则可以使用for循环遍历数组中的对象:

If you assign the provided code to a variable named array, you can use this code to iterate over the items in the array. 如果将提供的代码分配给名为array的变量,则可以使用此代码遍历数组中的项目。

Once you do that, you can then access the properties of the objects inside using the dot operator, locating and updating the correct item. 完成此操作后,您就可以使用点运算符访问内部对象的属性,查找并更新正确的项目。

function echo (text){
    document.getElementById("output").innerHTML += text + "<br/>"
}


var array = [
  {
    "_id":68917,
    "Distance":8.81,
    "Lat":"42.65322175",
    "Lon":"-73.77398886"
  },
  {
    "_id":131277,
    "Distance":"9.86",
    "Lat":"42.654658",
    "Lon":"-73.805866"
  },
  {
    "_id":62450,
    "Distance":6.58,
    "Lat":"42.67457566",
    "Lon":"-73.74902171"
  }];


echo("First item's distance " + array[0].Distance);

for(var i = 0; i < array.length;  i++){
    if(array[i]._id === 68917){
        echo("Found item "+array[i]._id);
        array[i].Distance = 4.45;
    }
}

echo("First item's distance " + array[0].Distance);

I created a JS Fiddle that demonstrates this solution works; 我创建了一个JS小提琴来演示此解决方案的工作。 http://jsfiddle.net/gB5Nn/2/ http://jsfiddle.net/gB5Nn/2/

You could use the built-in "filter" function to get the item you want. 您可以使用内置的“过滤器”功能来获取所需的项目。

someObject.filter(function(item){return item._id === 68917;})[0].Distance = 4.5;

Here is an example: 这是一个例子:

http://jsfiddle.net/aVBx8/ http://jsfiddle.net/aVBx8/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅使用lodash或纯JavaScript添加新对象或更新现有对象属性? - How do you add new or update existing object properties using lodash only or plain javascript? Javascript - 如何向对象构造函数添加属性 - Javascript - how do you add properties to an object constructor function 如何在 javascript 中的纯 function 中更新 object? - How do you update an object in a pure function in javascript? 如何在不使用Javascript或jQuery添加属性的情况下将对象的属性复制到另一个对象? - How do you copy properties of an object into another without adding properties using Javascript or jQuery? 在Plain / Pure Javascript中,如何找出元素对象有哪些可用的方法或属性? - In Plain/Pure Javascript, how do you find out what available methods or properties an element object has? 您如何迭代JavaScript对象的属性(如数组)? - How do you iterate over a JavaScript object's properties like an array? 在Javascript中,如果有一个对象具有很多作为函数的属性,那么如何将它们转换为(函数名称的)字符串数组? - In Javascript, if there is an object with a lot of properties that are functions, how do you convert them to array of strings (of the function names)? 你如何更新 React 组件状态中对象的单个属性? - How do you update individual properties of objects in React component states? JavaScript更新多个对象属性 - JavaScript Update multiple object properties Javascript:如何在不更改代码的情况下更新对象值? - Javascript: How do you update a object value without changing your code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM