user5283721 user5283721 - 5 months ago 8x
Javascript Question

Extracting specific information from an array

I have data two sets of data as follows:

The first three parts are fixed and the remaining can extend to as many as possible.
I am trying to build an object where I want to split and combine whatever is present after three into an object.

var split = samplestr.split('.');
var finalarray = [];
finalarray[0] = split[0];
finalarray[1] = split[1];
finalarray[2] = split[2];
finalarray[3] = split[3]+"."split[4];

I need to generalise this such that even if the string is of the form


finalarray[3] = 1.2.3.Four;

Any hints on generalising this?


With Array#shift and Array#join.

var split = samplestr.split('.');
var finalarray = [];
if(split.length > 4) {
    finalarray[0] = split.shift();
    finalarray[1] = split.shift();
    finalarray[2] = split.shift();
    finalarray[3] = split.join(".");