簡體   English   中英

在javascript / jquery中更新對象數組,其中field是特定值

[英]Update array of objects in javascript / jquery where field is specific value

我有一個對象數組,如下所示:

var finishes = [
    {label:'Raw Steel'},
    {label:'Antique Pewter'},
    {label:'Barn Red'},
    {label:'Brushed Stainless Steel'},
    {label:'Brushed Steel'},
    {label:'Copper Patina'},
    {label:'Dark Bronze'},
    {label:'Distressed White'},
    {label:'Flat Black'},
    {label:'Green Patina'},
    {label:'Oil Rubbed Bronze'},
    {label:'White'},
    {label:'Warehouse Bronze'},
    {label:'Weathered Rust'},
];

var wheelFinishes = finishes;

正如您所看到的,我設置了另一個對象數組,這些對象將具有一些不同的屬性,然后是“種子”對象數組。

所以我想做的是:

UPDATE wheelFinishes WHERE label="Barn Red" SET exclusion="Metal Values"

因此,wheelFinishes的價值將最終為:

var wheelFinishes = [
    {label:'Raw Steel'},
    {label:'Antique Pewter'},
    {label:'Barn Red', exclusion:'Metal Values'},
    {label:'Brushed Stainless Steel'},
    {label:'Brushed Steel'},
    {label:'Copper Patina'},
    {label:'Dark Bronze'},
    {label:'Distressed White'},
    {label:'Flat Black'},
    {label:'Green Patina'},
    {label:'Oil Rubbed Bronze'},
    {label:'White'},
    {label:'Warehouse Bronze'},
    {label:'Weathered Rust'},
];

我不確定在javascript中更新對象數組的實際語法。

我知道下划線可能有一些功能可以使這種類型的東西更容易,所以如果可能的話,我可以接受下划線的解決方案嗎?

遍歷數組,並追加屬性。

使用功能update 我接受一組需要修改的標簽元素。 在每組標簽下,還有另一組要追加。

 var finishes = [{ label: 'Raw Steel' }, { label: 'Antique Pewter' }, { label: 'Barn Red' }, { label: 'Brushed Stainless Steel' }, { label: 'Brushed Steel' }, { label: 'Copper Patina' }, { label: 'Dark Bronze' }, { label: 'Distressed White' }, { label: 'Flat Black' }, { label: 'Green Patina' }, { label: 'Oil Rubbed Bronze' }, { label: 'White' }, { label: 'Warehouse Bronze' }, { label: 'Weathered Rust' }, ]; function update(arr) { var i, len, len2, len3, elem, j, k, key, value; for (i = 0, len = arr.length; i < len; i += 1) { elem = arr[i]; for (j = 0, len2 = finishes.length; j < len2; j += 1) { if (finishes[j].label === elem.label) { for (k = 0, len3 = elem.set.length; k < len3; k += 1) { key = elem.set[k].key; value = elem.set[k].value; finishes[j][key] = value; } } } } return finishes; } console.log(update([{ label: 'Barn Red', set: [{ key: 'exclusion1', value: 'Metal Values1' }, { key: 'exclusion2', value: 'Metal Values2' }] }])); 

使用Array.prototype.map將是一個(多個)可能的解決方案:

 var wheelFinishes = [ {label:'Raw Steel'}, {label:'Antique Pewter'}, {label:'Barn Red'}, {label:'Brushed Stainless Steel'}, {label:'Brushed Steel'}, {label:'Copper Patina'}, {label:'Dark Bronze'}, {label:'Distressed White'}, {label:'Flat Black'}, {label:'Green Patina'}, {label:'Oil Rubbed Bronze'}, {label:'White'}, {label:'Warehouse Bronze'}, {label:'Weathered Rust'}, ]; //extend all objects having a specific label updatedFinishes = wheelFinishes.map(function(obj) { if(obj.label === 'Barn Red') { obj.exclusion = 'Metal Values'; } return obj; }); //test updatedFinishes.forEach(function(obj) { console.log(obj); }) 

請注意,與map()不同,這僅適用於數組中帶有{label: 'Barn Red'}的第一個項目。

正如@RobertRocha所建議的那樣:

var wheelFinishes = finishes.slice();
wheelFinishes.find(function (finish) {
  return finish.label === 'Barn Red';
}).exclusion = 'Metal Values';

這將使用slice 復制數組,然后使用.label === 'Barn Red' 找到該項目。


在ES6中:

const wheelFinishes = finishes.slice();
wheelFinishes
  .find(finish => finish.label === 'Barn Red')
  .exclusion = 'Metal Values';

使其與您的概念兼容

UPDATE wheelFinishes WHERE label =“Barn Red”SET exclusion =“Metal Values”

並使其盡可能松散耦合/獨立,這是一個可以幫助您的功能:

    var finishes = [
    {label:'Raw Steel'},
    {label:'Antique Pewter'},
    {label:'Barn Red'},
    {label:'Brushed Stainless Steel'},
    {label:'Brushed Steel'},
    {label:'Copper Patina'},
    {label:'Dark Bronze'},
    {label:'Distressed White'},
    {label:'Flat Black'},
    {label:'Green Patina'},
    {label:'Oil Rubbed Bronze'},
    {label:'White'},
    {label:'Warehouse Bronze'},
    {label:'Weathered Rust'},
    ];

    var wheelFinishes = finishes;

    function update(table, searchItem, setValue) {
        function search(table) {
            if(table.label == searchItem) {
                table.exclusion = setValue;
            }
        }

        table.find(search);
    }

    update(wheelFinishes, 'Barn Red', 'Metal Values');

    console.log(wheelFinishes[2]);

結果: Object {label: "Barn Red", exclusion: "Metal Values"}

您可以使用例如常規forEach

 var finishes = [ {label:'Raw Steel'}, {label:'Antique Pewter'}, {label:'Barn Red'}, {label:'Brushed Stainless Steel'}, {label:'Brushed Steel'}, {label:'Copper Patina'}, {label:'Dark Bronze'}, {label:'Distressed White'}, {label:'Flat Black'}, {label:'Green Patina'}, {label:'Oil Rubbed Bronze'}, {label:'White'}, {label:'Warehouse Bronze'}, {label:'Weathered Rust'}, ]; finishes.forEach(function(f) { if(f.label === 'Barn Red') { f.exclusion = 'Metal Values'; } }); console.log(finishes); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM