2

I found this post but it's not really what I'm looking for. I have a huge JSON file with this structure:

{
    foo: [1, 2, 3, ...],
    bar: [
        {
            name: 'abc',
            cl: 2,
            data: [
                      [[2, 4, 6], [4, 5, 6]],
                      [[5, 3, 5], [5, 7, 9]],
                      [[6, 8, 9], [6, 8, 9]],
                      ...
                  ]
        },
        {
            name: 'def',
            cl: 1,
            data: [10, 20, 30, ...]
        }
    ]
}

I have a class representing the object and I can extract the properties and the objects inside the parent object. The foo property is mandatory while the bar property is optional. data is a numeric array that can be 1D or 3D. The foo.length and the data.length are always the same. I want to reduce the file size (in term of contents) but keeping the same structure, i.e. copy the old object to a new with exactly the same structure but shorter, so I could define a smaller length or range and get a new object, e.g. if I say to go from foo[0] to foo[1] then the data property will also go from data[0] to data[1] in the new object.

Is this possible? If yes, how can I achieve it? Maybe with map? but how?

function filter(obj, from, to){
     /// ?
}

Regards.

EDIT:

The new reduced object should be something like this:

{
    foo: [1, 2],
    bar: [
    {
        name: 'abc',
        cl: 2,
        data: [
                  [[2, 4, 6], [4, 5, 6]],
                  [[5, 3, 5], [5, 7, 9]]
              ]
    },
    {
        name: 'def',
        cl: 1,
        data: [10, 20]
    }
]
}
5
  • How exactly you want to reduce the file size? Commented Nov 7, 2016 at 9:00
  • please add some use cases. Commented Nov 7, 2016 at 9:06
  • I just edit the question. Commented Nov 7, 2016 at 9:30
  • What is the rule? Take the first two elements? Commented Nov 7, 2016 at 9:40
  • No, I should be able to take the elements from a point to another, so in this example, I want to go from foo[0] to foo[1]. But it could be any range inside the length of the foo.length. That will also affect the elements taken from data: foo.length = (bar with name 'abc').data.length = (bar with name 'def').data.length And foo[i] is related with (bar with name 'abc').data[i] and (bar with name 'def').data[i] Commented Nov 7, 2016 at 9:51

1 Answer 1

1

You could use Array#slice for it.

function slice(obj, from, to) {
    obj.foo = obj.foo.slice(from, to + 1);
    obj.bar.forEach(function (a) {
        a.data = a.data.slice(from, to + 1);
    });
}

var object = { foo: [1, 2, 3], bar: [{ name: 'abc', cl: 2, data: [[[2, 4, 6], [4, 5, 6]], [[5, 3, 5], [5, 7, 9]], [[6, 8, 9], [6, 8, 9]], ] }, { name: 'def', cl: 1, data: [10, 20, 30] }] };

slice(object, 0, 1);
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.