我有一个javascript地图,其中我想分隔最后一组对象,
{
"97483": {
"_index": 0,
"text_html": "sadf"
},
"97484": {
"_index": 1,
"text_html": "sfhsdfasdfsdf"
},
"97485": {
"_index": 2,
"text_html": "test1"
},
"97486": {
"_index": 3,
"text_html": "test2"
},
"97487": {
"_index": 4,
"text_html": "test3"
},
"97493": {
"_index": 0,
"text_html": "test9"
},
"97494": {
"_index": 1,
"text_html": "test10"
},
"97495": {
"_index": 2,
"text_html": "test11"
},
"97496": {
"_index": 3,
"text_html": "test11"
},
"97893": {
"_index": 0,
"text_html": "test99"
},
"97894": {
"_index": 1,
"text_html": "test999"
},
"97895": {
"_index": 2,
"text_html": "test9999"
},
}
在这个javascript地图中我怎么能分开
"97893": {
"_index": 0,
"text_html": "test99"
},
"97894": {
"_index": 1,
"text_html": "test999"
},
"97895": {
"_index": 2,
"text_html": "test9999"
},
实际上,主要的动机是将_index从0开始的最后一组元素分开。 任何帮助将不胜感激。
答案 0 :(得分:0)
尝试编写代码,
var mainObj = {...}; //your object
var result = {};
Object.keys(mainObj).forEach(function(itm){
if(mainObj[itm]._index == 0) result = {};
result[itm] = mainObj[itm];
});
上面的代码将从主对象中收集最后一组对象。
答案 1 :(得分:0)
无序财产的提案。
var object = { "97483": { "_index": 0, "text_html": "sadf" }, "97484": { "_index": 1, "text_html": "sfhsdfasdfsdf" }, "97485": { "_index": 2, "text_html": "test1" }, "97486": { "_index": 3, "text_html": "test2" }, "97487": { "_index": 4, "text_html": "test3" }, "97493": { "_index": 0, "text_html": "test9" }, "97494": { "_index": 1, "text_html": "test10" }, "97495": { "_index": 2, "text_html": "test11" }, "97496": { "_index": 3, "text_html": "test11" }, "97893": { "_index": 0, "text_html": "test99" }, "97894": { "_index": 1, "text_html": "test999" }, "97895": { "_index": 2, "text_html": "test9999" } },
keys = Object.keys(object).map(Number).sort((a, b) => a - b),
key = keys.filter(a => object[a]._index === 0),
result = {};
keys.forEach(function (a) {
if (a >= this) {
result[a] = object[a];
}
}, key[key.length - 1]);
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
&#13;
答案 2 :(得分:0)
可以在赋值指令中使用纯JS来完成。
var dataObj = { "97483": { "_index": 0, "text_html": "sadf" }, "97484": { "_index": 1, "text_html": "sfhsdfasdfsdf" }, "97485": { "_index": 2, "text_html": "test1" }, "97486": { "_index": 3, "text_html": "test2" }, "97487": { "_index": 4, "text_html": "test3" }, "97493": { "_index": 0, "text_html": "test9" }, "97494": { "_index": 1, "text_html": "test10" }, "97495": { "_index": 2, "text_html": "test11" }, "97496": { "_index": 3, "text_html": "test11" }, "97893": { "_index": 0, "text_html": "test99" }, "97894": { "_index": 1, "text_html": "test999" }, "97895": { "_index": 2, "text_html": "test9999" } },
ok = Object.keys(dataObj),
reduced = ok.fill(false,0,ok.reduce((p,c) => (p.push(dataObj[c]._index),p),[]).lastIndexOf(0)).reduce((p,c) => (c && (p[c] = dataObj[c]),p),{});
document.write("<pre>" + JSON.stringify(reduced,null,2) + "</pre>");
&#13;