gpt4 book ai didi

javascript - 从嵌套对象中删除父键

转载 作者:行者123 更新时间:2023-11-28 10:44:43 24 4
gpt4 key购买 nike

我正在尝试对一个对象进行排序,然后使用排序后的数据创建一个新对象。但问题是,每当我将数据推送到空数组时,它都会创建一个父键为 0,1,2... 的新对象。

{
"0": {
"note-1502256197947": {
"content": "If that doesn't work, then it will reload the entire page.",
"fav": true,
"last_modified": "",
"publish_date": 1502256197947,
"slug": "starred-note",
"title": "Starred Note"
}
},
"1": {
"note-1502108241905": {
"categories": {
"Learn": true,
"Wordpress": true
},
"content": "Creating a embed several routes to handle Creation, Retrieval",
"fav": true,
"last_modified": "",
"publish_date": 1502108241905,
"slug": "a-new-title",
"title": "A New Title"
}
}
}

这是我尝试对对象进行排序的方法:

      var noteref = firebase.database().ref(`noted${uid}/notes/`);
var sortedKeys = [];

noteref.orderByChild('fav').once("value", function(snapshot) {

snapshot.forEach(function(child) {

sortedKeys.unshift({[child.key]:child.val()});
//sortedKeys[child.key] = child.val(); ***Works! but the order is lost
});

var newObject = {...sortedKeys};
console.log(newObject);
});

这就是我尝试重新创建对象的方式:

{
"note-1502256197947": {
"content": "If that doesn't work, then it will reload the entire page.",
"fav": true,
"last_modified": "",
"publish_date": 1502256197947,
"slug": "starred-note",
"title": "Starred Note"

},
"note-1502108241905": {
"categories": {
"Learn": true,
"Wordpress": true
},
"content": "Creating a embed several routes to handle Creation, Retrieval",
"fav": true,
"last_modified": "",
"publish_date": 1502108241905,
"slug": "a-new-title",
"title": "A New Title"
}

}

最佳答案

我认为您缺少一些对象的概念。如果一个对象有几个属性,那么并不意味着它们以任何方式排序。

  {
"note-1502256197947": {...
},
"note-1502108241905": {...
}
}

假设这是一个对象。该对象有 2 个属性 note-1502256197947note-1502108241905 。这并不意味着它们已排序。

关于javascript - 从嵌套对象中删除父键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45589581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com