0

I've an javascript map in which i would like to separate the last set of objects,

{
    "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"
    },

}

In this javascript map how i can separate out the

"97893": {
        "_index": 0,
        "text_html": "test99"
    },
    "97894": {
        "_index": 1,
        "text_html": "test999"
    },
    "97895": {
        "_index": 2,
        "text_html": "test9999"
    },

Actually the main motive is to separate out the last set of elements whose _index starts from 0. Any help would be greatly appreciated.

user3588408
  • 301
  • 6
  • 9

3 Answers3

0

Try to write your code like,

var mainObj = {...}; //your object
var result = {};

Object.keys(mainObj).forEach(function(itm){
 if(mainObj[itm]._index == 0) result = {};
 result[itm] = mainObj[itm];
});

The above code will collect the last set of objects from the main object.

DEMO

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

A proposal for unordered properties.

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>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Can be done with pure JS in an assignment instruction.

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>");
Redu
  • 25,060
  • 6
  • 56
  • 76