Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

Đệ quy là bạn của bạn. Tôi đã cập nhật chức năng để tính toán các mảng thuộc tính:

function getObject(theObject) {
    var result = null;
    if(theObject instanceof Array) {
        for(var i = 0; i < theObject.length; i++) {
            result = getObject(theObject[i]);
            if (result) {
                break;
            }   
        }
    }
    else
    {
        for(var prop in theObject) {
            console.log(prop + ': ' + theObject[prop]);
            if(prop == 'id') {
                if(theObject[prop] == 1) {
                    return theObject;
                }
            }
            if(theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
                result = getObject(theObject[prop]);
                if (result) {
                    break;
                }
            } 
        }
    }
    return result;
}

Cập nhật jsfiddle: http://jsfiddle.net/fm3qu/7/

Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

Đã trả lời ngày 20 tháng 3 năm 2013 lúc 13:02Mar 20, 2013 at 13:02

ZachzachZach

3.02817 Huy hiệu bạc32 Huy hiệu đồng17 silver badges32 bronze badges

11

Điều làm việc cho tôi là cách tiếp cận lười biếng này, không lười biếng về mặt thuật toán;)

if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}

Đã trả lời ngày 25 tháng 10 năm 2018 lúc 7:50Oct 25, 2018 at 7:50

Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

abhinav1602abhinav1602abhinav1602

1.13016 huy hiệu bạc18 Huy hiệu đồng16 silver badges18 bronze badges

5

Một tùy chọn khác (hơi ngớ ngẩn) là khai thác bản chất đệ quy tự nhiên của

function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i
5 và chuyển nó một hàm thay thế chạy trên mỗi đối tượng lồng nhau trong quá trình chuỗi:

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};

Đã trả lời ngày 19 tháng 5 năm 2019 lúc 2:55May 19, 2019 at 2:55

5

Nếu bạn muốn nhận phần tử đầu tiên có ID là 1 trong khi đối tượng đang được tìm kiếm, bạn có thể sử dụng chức năng này:

function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i

Nếu bạn muốn nhận tất cả các yếu tố có ID là 1, thì tất cả các yếu tố có ID là 1 được lưu trữ trong kết quả như bạn thấy):

function customFilter(object, result){
    if(object.hasOwnProperty('id') && object.id == 1)
        result.push(object);

    for(var i=0; i

Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

Zalog

6519 Huy hiệu bạc20 Huy hiệu Đồng9 silver badges20 bronze badges

Đã trả lời ngày 20 tháng 3 năm 2013 lúc 12:34Mar 20, 2013 at 12:34

Haitakahaitakahaitaka

1.80212 huy hiệu bạc21 Huy hiệu đồng12 silver badges21 bronze badges

1

Cải thiện câu trả lời @haitaka, sử dụng khóa và vị ngữ

function  deepSearch (object, key, predicate) {
    if (object.hasOwnProperty(key) && predicate(key, object[key]) === true) return object

    for (let i = 0; i < Object.keys(object).length; i++) {
      let value = object[Object.keys(object)[i]];
      if (typeof value === "object" && value != null) {
        let o = deepSearch(object[Object.keys(object)[i]], key, predicate)
        if (o != null) return o
      }
    }
    return null
}

Vì vậy, điều này có thể được gọi như:

var result = deepSearch(myObject, 'id', (k, v) => v === 1);

hoặc

var result = deepSearch(myObject, 'title', (k, v) => v === 'Some Recommends');

Đây là bản demo: http://jsfiddle.net/a21dx6c0/

Đã chỉnh sửa

Theo cùng một cách bạn có thể tìm thấy nhiều hơn một đối tượng

function deepSearchItems(object, key, predicate) {
        let ret = [];
        if (object.hasOwnProperty(key) && predicate(key, object[key]) === true) {
            ret = [...ret, object];
        }
        if (Object.keys(object).length) {
            for (let i = 0; i < Object.keys(object).length; i++) {
                let value = object[Object.keys(object)[i]];
                if (typeof value === "object" && value != null) {
                    let o = this.deepSearchItems(object[Object.keys(object)[i]], key, predicate);
                    if (o != null && o instanceof Array) {
                        ret = [...ret, ...o];
                    }
                }
            }
        }
        return ret;
    }

Đã trả lời ngày 31 tháng 1 năm 2019 lúc 23:44Jan 31, 2019 at 23:44

2

Tôi tìm thấy trang này thông qua Googling cho các chức năng tương tự. Dựa trên công việc được cung cấp bởi Zach và CreatMike, tôi đã tạo một phiên bản khác phù hợp với nhu cầu của tôi. BTW, Teriffic làm việc Zah và CreatMike! Tôi sẽ đăng mã ở đây:
BTW, teriffic work Zah and regularmike! I'll post the code here:

function findObjects(obj, targetProp, targetValue, finalResults) {

  function getObject(theObject) {
    let result = null;
    if (theObject instanceof Array) {
      for (let i = 0; i < theObject.length; i++) {
        getObject(theObject[i]);
      }
    }
    else {
      for (let prop in theObject) {
        if(theObject.hasOwnProperty(prop)){
          console.log(prop + ': ' + theObject[prop]);
          if (prop === targetProp) {
            console.log('--found id');
            if (theObject[prop] === targetValue) {
              console.log('----found porop', prop, ', ', theObject[prop]);
              finalResults.push(theObject);
            }
          }
          if (theObject[prop] instanceof Object || theObject[prop] instanceof Array){
            getObject(theObject[prop]);
          }
        }
      }
    }
  }

  getObject(obj);

}

Những gì nó làm là nó tìm thấy bất kỳ đối tượng nào bên trong

function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i
6 với tên thuộc tính và giá trị khớp với
function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i
7 và
function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i
8 và sẽ đẩy nó vào mảng
function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i
9. Và đây là jsfiddle để chơi xung quanh: https://jsfiddle.net/alexqch/5u6q2ybc/

Đã trả lời ngày 27 tháng 3 năm 2017 lúc 14:10Mar 27, 2017 at 14:10

Alex Quanalex QuanAlex Quan

811 Huy hiệu bạc3 Huy hiệu đồng1 silver badge3 bronze badges

1

Tôi đã tạo thư viện cho mục đích này: https://github.com/dominik791/obj-taverse

Bạn có thể sử dụng phương thức

function customFilter(object, result){
    if(object.hasOwnProperty('id') && object.id == 1)
        result.push(object);

    for(var i=0; i
0 như thế này:

if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}
0

Và bây giờ biến

function customFilter(object, result){
    if(object.hasOwnProperty('id') && object.id == 1)
        result.push(object);

    for(var i=0; i
1 lưu trữ một tham chiếu đến đối tượng mà bạn đang tìm kiếm.

Đã trả lời ngày 14 tháng 2 năm 2017 lúc 12:51Feb 14, 2017 at 12:51

dominik791dominik791dominik791

6925 Huy hiệu bạc16 Huy hiệu Đồng5 silver badges16 bronze badges

Nếu bạn thích toàn bộ điều ES6, bạn có thể sử dụng

if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}
1

Tìm theo giá trị sẽ khác một chút

if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}
2

Sau đó, chúng có thể được sử dụng như thế này

if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}
3

Đã trả lời ngày 26 tháng 11 năm 2021 lúc 20:37Nov 26, 2021 at 20:37

Olleolleolle

2861 Huy hiệu bạc5 Huy hiệu đồng1 silver badge5 bronze badges

Một giải pháp đệ quy khác, hoạt động cho các mảng/danh sách và đối tượng, hoặc hỗn hợp của cả hai:

if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}
4

usage:

if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}
5

Đã trả lời ngày 27 tháng 10 năm 2020 lúc 17:11Oct 27, 2020 at 17:11

Ali Alnoaimiali AlnoaimiAli Alnoaimi

2.3002 huy hiệu vàng21 Huy hiệu bạc31 Huy hiệu đồng2 gold badges21 silver badges31 bronze badges

1

Chúng tôi sử dụng quét đối tượng để xử lý dữ liệu của chúng tôi. Nó rất đơn giản về mặt khái niệm, nhưng cho phép rất nhiều thứ thú vị. Đây là cách bạn sẽ giải quyết câu hỏi cụ thể của mình

if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}
6
if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}
7
if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}
8

Tuyên bố miễn trừ trách nhiệm: Tôi là tác giả của đối tượng quét

Đã trả lời ngày 7 tháng 10 năm 2019 lúc 3:52Oct 7, 2019 at 3:52

Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

Vincentvincentvincent

1.7681 Huy hiệu vàng16 Huy hiệu bạc22 Huy hiệu đồng1 gold badge16 silver badges22 bronze badges

2

Bạn có thể sử dụng hàm JavaScript

function customFilter(object, result){
    if(object.hasOwnProperty('id') && object.id == 1)
        result.push(object);

    for(var i=0; i
2 bên trong hàm đệ quy. Ưu điểm của một số người là ngừng lặp lại một khi đứa trẻ được thành lập. Không sử dụng bản đồ sẽ chậm trong dữ liệu lớn.

if( JSON.stringify(object_name).indexOf("key_name") > -1 ) {
    console.log("Key Found");
}
else{
    console.log("Key not Found");
}
9

Đã trả lời ngày 5 tháng 3 năm 2021 lúc 14:07Mar 5, 2021 at 14:07

Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

0

Câu trả lời của @iulian Pinzaru gần như chính xác những gì tôi cần, nhưng nó không hoạt động nếu đối tượng của bạn có bất kỳ giá trị null nào. Phiên bản này sửa chữa điều đó.

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};
0

Đã trả lời ngày 10 tháng 12 năm 2019 lúc 18:01Dec 10, 2019 at 18:01

VillarrealizedVillarrealizedVillarrealized

8271 Huy hiệu vàng6 Huy hiệu bạc13 Huy hiệu đồng1 gold badge6 silver badges13 bronze badges

Chỉ cần sử dụng chức năng đệ quy. Xem ví dụ dưới đây:
See example below:

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};
1

Đã trả lời ngày 20 tháng 7 năm 2020 lúc 13:53Jul 20, 2020 at 13:53

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};
2

  1. Phát hiện chìa khóa trong đối tượng lồng sâu.
  2. Cuối cùng trả về giá trị của khóa được phát hiện.

Đã trả lời ngày 22 tháng 4 năm 2021 lúc 14:26Apr 22, 2021 at 14:26

Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

SuryasuryaSurya

Phù hiệu bằng đồng 3155 bronze badges

Cải thiện câu trả lời để tính đến các tài liệu tham khảo tròn trong các đối tượng. Nó cũng hiển thị con đường cần thiết để đến đó.

Trong ví dụ này, tôi đang tìm kiếm một iframe mà tôi biết là ở đâu đó trong một đối tượng toàn cầu:

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};
3

Chạy như sau:

function customFilter(object, result){
    if(object.hasOwnProperty('id') && object.id == 1)
        result.push(object);

    for(var i=0; i
3 đã đưa ra đầu ra sau và phần tử iframe cuối cùng:

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};
4

Lưu ý: Đường dẫn theo thứ tự ngược lại

function customFilter(object, result){
    if(object.hasOwnProperty('id') && object.id == 1)
        result.push(object);

    for(var i=0; i
4

Đã trả lời ngày 7 tháng 4 năm 2019 lúc 18:50Apr 7, 2019 at 18:50

Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

GerardlamogerardlamoGerardlamo

1.34512 Huy hiệu bạc19 Huy hiệu đồng12 silver badges19 bronze badges

Tôi muốn đề xuất một sửa đổi đối với câu trả lời của Zach/CreatMike (nhưng không có "danh tiếng" để có thể bình luận!). Tôi đã tìm thấy giải pháp một cơ sở rất hữu ích, nhưng phải chịu trong ứng dụng của mình bởi vì nếu có các chuỗi trong các mảng, nó sẽ gọi lại hàm cho mỗi ký tự trong chuỗi (khiến các trình duyệt IE11 & Edge không lỗi với lỗi "Không gian ngăn xếp" ). Tối ưu hóa đơn giản của tôi là thêm cùng một thử nghiệm được sử dụng trong lệnh gọi đệ quy "đối tượng" vào một điều khoản trong mệnh đề "mảng": điều khoản:

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};
5

Do đó, mã đầy đủ của tôi (hiện đang tìm kiếm tất cả các trường hợp của một khóa cụ thể, rất khác với yêu cầu ban đầu) là:

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};
6

Đã trả lời ngày 23 tháng 9 năm 2019 lúc 13:53Sep 23, 2019 at 13:53

Cách đây một thời gian, tôi đã tạo ra một lib nhỏ

function customFilter(object, result){
    if(object.hasOwnProperty('id') && object.id == 1)
        result.push(object);

    for(var i=0; i
5, có sẵn trên NPM, để làm việc với các đối tượng lồng nhau theo cách thức của Lodash. Có chức năng
function customFilter(object, result){
    if(object.hasOwnProperty('id') && object.id == 1)
        result.push(object);

    for(var i=0; i
6 trả về đối tượng tìm thấy hoặc một mảng đối tượng nếu có nhiều hơn một đối tượng được tìm thấy.

E.g.,

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};
7

trả lại

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};
8

Đã trả lời ngày 29 tháng 1 năm 2020 lúc 13:03Jan 29, 2020 at 13:03

ArfeoarfeoArfeo

8607 Huy hiệu bạc20 Huy hiệu Đồng7 silver badges20 bronze badges

Tìm thấy câu trả lời mà tôi đang tìm kiếm, đặc biệt là giải pháp của Ali Alnoaimi. Tôi cũng đã thực hiện một số điều chỉnh nhỏ để cho phép tìm kiếm giá trị

const input = [{
  'title': "some title",
  'channel_id': '123we',
  'options': [{
    'channel_id': 'abc',
    'image': 'http://asdasd.com/all-inclusive-block-img.jpg',
    'title': 'All-Inclusive',
    'options': [{
      'channel_id': 'dsa2',
      'title': 'Some Recommends',
      'options': [{
        'image': 'http://www.asdasd.com',
        'title': 'Sandals',
        'id': '1',
        'content': {}
      }]
    }]
  }]
}];

console.log(findNestedObj(input, 'id', '1'));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};
9

Để sử dụng:

function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i
0

Điều này sẽ trả về đối tượng chứa khóa và giá trị phù hợp.

Đã trả lời ngày 12 tháng 2 lúc 5:14Feb 12 at 5:14

Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

FuzufuzuFuZu

Phù hiệu bằng đồng 1133 bronze badges

0

function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i
1

Bạn được chào đón bởi: Gorillaz

Đã trả lời ngày 12 tháng 11 năm 2020 lúc 17:32Nov 12, 2020 at 17:32

Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

1

Phần mã này cho phép bạn có được tất cả các đối tượng trong JSON có khóa được xác định bởi người dùng.

function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i
2

Đây là một ví dụ:

function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i
3

Đã trả lời ngày 6 tháng 5 năm 2020 lúc 13:19May 6, 2020 at 13:19

Hướng dẫn find object in nested object javascript - tìm đối tượng trong javascript đối tượng lồng nhau

LuispluispLuisP

11 Huy hiệu Đồng1 bronze badge

Nếu bạn đã sử dụng dấu gạch dưới, hãy sử dụng _.find ()

function customFilter(object){
    if(object.hasOwnProperty('id') && object["id"] == 1)
        return object;

    for(var i=0; i
4

Đã trả lời ngày 20 tháng 3 năm 2013 lúc 12:41Mar 20, 2013 at 12:41

Rhodesjasonrhodesjasonrhodesjason

4.8359 Huy hiệu vàng43 Huy hiệu bạc58 Huy hiệu Đồng9 gold badges43 silver badges58 bronze badges

2

Làm cách nào để truy cập các đối tượng lồng nhau?

Cấu trúc dữ liệu lồng nhau là một mảng hoặc đối tượng đề cập đến các mảng hoặc đối tượng khác, tức là các giá trị của nó là mảng hoặc đối tượng. Các cấu trúc như vậy có thể được truy cập bằng cách áp dụng liên tiếp ký hiệu dấu chấm hoặc khung.consecutively applying dot or bracket notation.

Làm cách nào để truy cập các mảng lồng nhau?

Để truy cập một phần tử của mảng đa chiều, trước tiên bạn sử dụng dấu ngoặc vuông để truy cập một phần tử của mảng bên ngoài trả về một mảng bên trong;và sau đó sử dụng một khung vuông khác để truy cập phần tử của mảng bên trong.use square brackets to access an element of the outer array that returns an inner array; and then use another square bracket to access the element of the inner array.

Làm thế nào để bạn truy cập dữ liệu từ một mảng các đối tượng?

Làm thế nào để bạn nhận được các giá trị từ mảng các đối tượng trong JS ?..
Sử dụng mảng.nguyên mẫu.tìm () chức năng ..
Sử dụng mảng.nguyên mẫu.findIndex () hàm ..
Sử dụng mảng.nguyên mẫu.chức năng foreach () ..
Sử dụng mảng.nguyên mẫu..
Sử dụng jQuery.Số tiền của jQuery ..
Sử dụng thư viện Lodash/Undercore.Thư viện dấu gạch dưới và lodash có _ ..

Đối tượng lồng nhau trong JavaScript là gì?

Định nghĩa cơ bản của một đối tượng trong JavaScript là một thùng chứa cho các giá trị được đặt tên gọi là Thuộc tính (khóa).Đôi khi, chúng ta cần tạo một đối tượng bên trong một đối tượng khác.Trong trường hợp này, nó được gọi là một đối tượng lồng nhau.create an object inside another object. In this case, it's called a nested object.