1
var caseOne = [
    {"name":"Felicity", "gender":"F", ...   ... ,"type":"Admin"},
    {"name":"Tony", "gender":"M", ...   ... ,"type":""},
    .
    .
    .
    .
    {"name":"Super Man", "gender":"M", ...   ... ,"type":""},
    {"name":"Wonder Women", "gender":"F", ...   ... ,"type":""},
    {"name":"Hulk", "gender":"M", ...   ... ,"type":"User"},
    {"name":"Loky", "gender":"M", ...   ... ,"type":"User"},
    {"name":"Thore", "gender":"M", ...   ... ,"type":"Admin"}
];

var caseTwo = [
    {"name":"Felicity", "gender":"F", ...   ... ,"type":"Admin"},
    {"name":"Tony", "gender":"M", ...   ... ,"type":""},
    .
    .
    .
    .
    {"name":"Super Man", "gender":"M", ...   ... ,"type":""},
    {"name":"Wonder Women", "gender":"F", ...   ... ,"type":""},
    {"name":"Hulk", "gender":"M", ...   ... ,"type":"User"},
    {"name":"Loky", "gender":"M", ...   ... ,"type":"User"},
    {"name":"Thore", "gender":"M", ...   ... ,"type":"Admin"},
    {"name":"Bat man", "gender":"M", ...   ... ,"type":""}
];

I have this kind of array. I want all records after lastIndexOf "type":"", means From cansOne it'll return last 3 record and from caseTwo it'll return 0 record. in short all records after "type":"". Can anyone help.

Thanks in advance.

Expected output

caseOne = [
    {"name":"Hulk", "gender":"M", ...   ... ,"type":"User"},
    {"name":"Loky", "gender":"M", ...   ... ,"type":"User"},
    {"name":"Thore", "gender":"M", ...   ... ,"type":"Admin"}
];

caseTwo = [];
3
  • what you want to achieve? Commented Apr 27, 2018 at 11:39
  • @Parth Raval, I want last all records which has type. means if last record has type:"" then it return 0 or if third last record has type:"" then it'll return last two records. Commented Apr 27, 2018 at 11:46
  • @VipulSolanki find the index and splice it. Check the answer Commented Apr 27, 2018 at 11:51

3 Answers 3

3

Create a reusable function getIndex() that will take a array parameter and give you the last index of the object with type: "". Using this index you can splice() the result and get the desired output

var caseOne = [
    {"name":"Felicity", "gender":"F" ,"type":"Admin"},
    {"name":"Tony", "gender":"M","type":""},
    {"name":"Super Man", "gender":"M","type":""},
    {"name":"Wonder Women", "gender":"F" ,"type":""},
    {"name":"Hulk", "gender":"M","type":"User"},
    {"name":"Loky", "gender":"M" ,"type":"User"},
    {"name":"Thore", "gender":"M" ,"type":"Admin"}
];

var caseTwo = [
    {"name":"Felicity", "gender":"F" ,"type":"Admin"},
    {"name":"Tony", "gender":"M" ,"type":""},
    {"name":"Super Man", "gender":"M" ,"type":""},
    {"name":"Wonder Women", "gender":"F","type":""},
    {"name":"Hulk", "gender":"M","type":"User"},
    {"name":"Loky", "gender":"M","type":"User"},
    {"name":"Thore", "gender":"M","type":"Admin"},
    {"name":"Bat man", "gender":"M","type":""}
];

function getIndex(caseArray){
  var arrayIndex;
  caseArray.forEach(function(caseOneObj, index){
    if(caseOneObj.type === ''){
      arrayIndex = index;
    }
  });
  return arrayIndex;
}

var index = getIndex(caseOne);
var resultCaseOne = caseOne.splice(index+1, caseOne.length);
console.log('-----caseOne-----');
console.log(resultCaseOne);

index = getIndex(caseTwo);
var resultCaseTwo = caseTwo.splice(index+1, caseTwo.length);
console.log('-----caseTwo-----');
console.log(resultCaseTwo);

But, if you have array of objects that are most likely to have type:'' at the last of the array then you can search for that index starting from the last object of that array, like this:

var caseOne = [
    {"name":"Felicity", "gender":"F" ,"type":"Admin"},
    {"name":"Tony", "gender":"M","type":""},
    {"name":"Super Man", "gender":"M","type":""},
    {"name":"Wonder Women", "gender":"F" ,"type":""},
    {"name":"Hulk", "gender":"M","type":"User"},
    {"name":"Loky", "gender":"M" ,"type":"User"},
    {"name":"Thore", "gender":"M" ,"type":"Admin"}
];

var caseTwo = [
    {"name":"Felicity", "gender":"F" ,"type":"Admin"},
    {"name":"Tony", "gender":"M" ,"type":""},
    {"name":"Super Man", "gender":"M" ,"type":""},
    {"name":"Wonder Women", "gender":"F","type":""},
    {"name":"Hulk", "gender":"M","type":"User"},
    {"name":"Loky", "gender":"M","type":"User"},
    {"name":"Thore", "gender":"M","type":"Admin"},
    {"name":"Bat man", "gender":"M","type":""}
];

function getIndex(caseArray){
  var i;
  for(i=caseArray.length-1; i>0; i--){
   if(caseArray[i].type === ''){
      break;
    }
  }
  return i;
}

var index = getIndex(caseOne);
var resultCaseOne = caseOne.splice(index+1, caseOne.length);
console.log('-----caseOne-----');
console.log(resultCaseOne);

index = getIndex(caseTwo);
var resultCaseTwo = caseTwo.splice(index+1, caseTwo.length);
console.log('-----caseTwo-----');
console.log(resultCaseTwo);

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

8 Comments

we could change forEach to iterate from the end? and return downwards records this wouldnt be much faster?
It depends. What if the type:'' is in first index of array with size 1000. At this time it should loop over 999 records. Both scenario is same, from last or from first. You cannot actually say this is better than that. And we need the index of the last element so both can be opted @PanosK
@PanosK your point was useful. I edited my answer to include that
Thanks, exactly I want like your second snippet. both are help full. @Ankit.
@VipulSolanki glad to help you
|
1

What will getting those array elements this way solve? Since there's propably a better way than searching through an array to find everything after a certain element. Anyways, you;re looking for code like:

// source data
const source = [
    {"name":"Felicity", "gender":"F", "type":"Admin"},
    {"name":"Tony", "gender":"M", "type":""},
    {"name":"Super Man", "gender":"M", "type":""},
    {"name":"Wonder Women", "gender":"F", "type":""},
    {"name":"Hulk", "gender":"M", "type":"User"},
    {"name":"Loky", "gender":"M", "type":"User"},
    {"name":"Thore", "gender":"M", "type":"Admin"}
];
const lastIndex = Array
	.from( source ) // Clone the array so reversing does not affect the source.
	.reverse() // Reverse the array so we can use findIndex instead of writing findLastIndex ourselves.
	.findIndex( entry => !entry.type ); // Get the index of the first element that has no type.
const result = source.slice( source.length - lastIndex, source.length ); // Slice the orginal array.
console.log( result );

4 Comments

There are two arrays for OP and using this code is duplicated for both the arrays. This cannot be taken as a optimal solution
Just wrapping it in a function and returning the sliced part will solve that. Anyways, I was trying to help, not diss your own solution if that's how it came across. Can't take back my upvote on your answer now anymore though.
If you really meant to answer, answer in a appropriate way and make it complete. I hear incompleteness from your previous comment. Anyways I also don't want to diss
I'll go get us some tequilas and i'll reread the answering rules.
0
You can do it like this:

   var caseOne = [
                    {"name":"Felicity", "gender":"F","type":"Admin"},
                    {"name":"Tony", "gender":"M","type":""},
                    {"name":"Super Man", "gender":"M","type":""},
                    {"name":"Wonder Women", "gender":"F","type":""},
                    {"name":"Hulk", "gender":"M","type":"User"},
                    {"name":"Loky", "gender":"M","type":"User"},
                    {"name":"Thore", "gender":"M","type":"Admin"}
                ];
                var result =[];
                var counterIndex = 0;
                for(var i=0; i<caseOne.length; i++) {
                    if(caseOne[i].type == '')
                    counterIndex++;
                }
                result = caseOne.splice(counterIndex+1, caseOne.length);
            console.log(result);

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.