2013-08-01 78 views
5

tôi đã sử dụng mã này để tìm phần yêu cầu từ các đối tượng json từ sJhonny's QuestionTìm và cập nhật trong đối tượng json lồng nhau

dữ liệu mẫu

TestObj = { 
    "Categories": [{ 
     "Products": [{ 
      "id": "a01", 
      "name": "Pine", 
      "description": "Short description of pine." 
     }, 
     { 
      "id": "a02", 
      "name": "Birch", 
      "description": "Short description of birch." 
     }, 
     { 
      "id": "a03", 
      "name": "Poplar", 
      "description": "Short description of poplar." 
     }], 
     "id": "A", 
     "title": "Cheap", 
     "description": "Short description of category A." 
    }, 
    { 
     "Product": [{ 
      "id": "b01", 
      "name": "Maple", 
      "description": "Short description of maple." 
     }, 
     { 
      "id": "b02", 
      "name": "Oak", 
      "description": "Short description of oak." 
     }, 
     { 
      "id": "b03", 
      "name": "Bamboo", 
      "description": "Short description of bamboo." 
     }], 
     "id": "B", 
     "title": "Moderate", 
     "description": "Short description of category B." 
    }] 
}; 

Chức năng tìm

function getObjects(obj, key, val) { 
    var objects = []; 
    for (var i in obj) { 
     if (!obj.hasOwnProperty(i)) continue; 
     if (typeof obj[i] == 'object') { 
      objects = objects.concat(getObjects(obj[i], key, val)); 
     } else if (i == key && obj[key] == val) { 
      objects.push(obj); 
     } 
    } 
    return objects; 
} 

Sử dụng như như vậy:

getObjects(TestObj, 'id', 'A'); // Returns an array of matching objects 

Mã này là để chọn đoạn phù hợp từ nguồn. Nhưng những gì tôi muốn là cập nhật đối tượng nguồn với giá trị mới và truy xuất đối tượng nguồn được cập nhật.

Tôi muốn một cái gì đó giống như

getObjects(TestObj, 'id', 'A', 'B'); // Returns source with updated value. (ie id:'A' updated to id:'B' in the returned object) 

Mã của tôi

function getObjects(obj, key, val, newVal) { 
    var newValue = newVal; 
    var objects = []; 
    for (var i in obj) { 
     if (!obj.hasOwnProperty(i)) continue; 
     if (typeof obj[i] == 'object') { 
      objects = objects.concat(getObjects(obj[i], key, val)); 
     } else if (i == key && obj[key] == val) { 
      obj[key] = 'qwe'; 
     } 
    } 
    return obj; 
} 

này hoạt động nếu tôi cho obj[key] = 'qwe'; nhưng nếu tôi thay đổi mã vào obj[key] = newValue; cập nhật của nó như là không xác định.

Tại sao vậy?

+2

obj [key] = newVal? – Virus721

+0

nếu không, nếu có điều kiện? – Okky

+0

Tôi không nhận được những gì bạn muốn làm. Bạn có muốn cập nhật đối tượng nguồn cùng một lúc khi bạn truy xuất một phần của nó không? oO – Virus721

Trả lời

7

Bạn quên để vượt qua newValue trong cuộc gọi lồng nhau

function getObjects(obj, key, val, newVal) { 
    var newValue = newVal; 
    var objects = []; 
    for (var i in obj) { 
     if (!obj.hasOwnProperty(i)) continue; 
     if (typeof obj[i] == 'object') { 
      objects = objects.concat(getObjects(obj[i], key, val, newValue)); 
     } else if (i == key && obj[key] == val) { 
      obj[key] = 'qwe'; 
     } 
    } 
    return obj; 
} 
+0

Oh! Làm thế nào bất cẩn của tôi. Cảm ơn người đàn ông – Okky

1
function getObjects(obj, key, val, newVal) { 
    for (var i in obj) { 
     if (!obj.hasOwnProperty(i)) continue; 
     if (i == key && obj[key] == val) { 
      obj[key] = newVal; 
     } 
    } 
    return obj 
} 

này sẽ thực hiện cập nhật inplace của một giá trị tìm thấy với newValue (newVal)

+0

Điều này sẽ không hoạt động nếu Im sẽ lưu vào thứ gì đó trong một json lồng nhau – Okky

1

này?

function update(obj, key, newVal) { 
    for(var i in obj) { 
     if(typeof obj[i] == 'object') { 
      update(obj[i], key, newVal)); 
     } else if(i === key) { 
      obj[i] = newVal; 
     } 
    } 
    return obj; 
} 
+0

Không, Đó là cặp khóa giá trị không trùng lặp có thể có nhiều bản sao – Okky

Các vấn đề liên quan