Get the list that is contained between two Data

-2

I have a Collection of n Number of Objects, in which a field is Boolean, as I could get all the objects that are between two objects that have the field in true: EJ:

$scope.lista = [
    {d1:false, d2:Hola0},
    {d1:false, d2:Hola1},
    {d1:true,  d2:Hola2},
    {d1:false, d2:Hola3},
    {d1:false, d2:Hola4},
    {d1:false, d2:Hola5},
    {d1:false, d2:Hola6},
    {d1:true,  d2:Hola7},
    {d1:false, d2:Hola8},
    {d1:false, d2:Hola9}] 

Then I need to recover:

{d1:true,  d2:Hola2},
{d1:false, d2:Hola3},
{d1:false, d2:Hola4},
{d1:false, d2:Hola5},
{d1:false, d2:Hola6},
{d1:true,  d2:Hola7}

In what way I could do it either in AngularJS or in C # .NET .. I thought I would do it with a couple of validations and extra variables in a for, but I'm looking for something more aesthetic, which I recommend .. Greetings and Thanks !!

    
asked by Anyel 10.03.2018 в 07:56
source

3 answers

1

What you are looking for you can do with javascript simple.

here an example:

var Array = [
    {d1:false, d2:"Hola0"},
    {d1:false, d2:"Hola1"},
    {d1:true,  d2:"Hola2"},
    {d1:false, d2:"Hola3"},
    {d1:false, d2:"Hola4"},
    {d1:false, d2:"Hola5"},
    {d1:false, d2:"Hola6"},
    {d1:true,  d2:"Hola7"},
    {d1:false, d2:"Hola8"},
    {d1:false, d2:"Hola9"}],
newArray = [],
isTrue = false;

for (i in Array) {
 if (isTrue) {
  newArray.push(Array[i]);
  if (Array[i].d1)
   break;
 } else {
  isTrue = Array[i].d1;
  if (isTrue)
   newArray.push(Array[i]);
 }
}
console.log(Array);
console.log(newArray);
    
answered by 11.03.2018 в 01:21
0

I do not know if it's the most elegant way but it can help you

First you filter the indices of the two true of the array of objects, these will serve as start and end parameters in the slice that you will do to the array lista

var arrayFiltered = []
var arrayResult = []

for (var i = 0; i < lista.length; i++) {
  if (lista[i].d1 === true) {
    arrayFiltered.push(i)
  }
}

var arrayResult = lista.slice(arrayFiltered[0], arrayFiltered[1]+1)
    
answered by 19.03.2018 в 12:01
-1

so I see these using angularjs, but you solve this very easily with the "filter" method of the prototype Array class as follows:

var newList = $scope.lista.filter(function(element){
   return item.d1 === true; //returna aqueos items que tienen la propiedad di como true
})

I recommend you visit for better understanding link

I hope this helps you, regards:)

    
answered by 12.03.2018 в 04:22