it-swarm.com.de

holt den Index von einem JSON-Objekt mit Wert

Dies ist meine Json-Saite.

[{
    "name": "placeHolder",
    "section": "right"
}, {
    "name": "Overview",
    "section": "left"
}, {
    "name": "ByFunction",
    "section": "left"
}, {
    "name": "Time",
    "section": "left"
}, {
    "name": "allFit",
    "section": "left"
}, {
    "name": "allbMatches",
    "section": "left"
}, {
    "name": "allOffers",
    "section": "left"
}, {
    "name": "allInterests",
    "section": "left"
}, {
    "name": "allResponses",
    "section": "left"
}, {
    "name": "divChanged",
    "section": "right"
}]

Jetzt habe ich den Wert allInterests und möchte den Index (in diesem Fall; es ist '7') dieses Objekts in der obigen Zeichenfolge ermitteln. Ich habe den folgenden Code ausprobiert; aber es gibt immer -1 zurück. Kann mir jemand helfen, herauszufinden, wo ich falsch lief?

var q = MY_JSON_STRING
console.log(q.indexOf( 'allInterests' ) );

Danke und Grüße, Tismon Varghese

9
Tismon Varghese

Sie müssen Array.find oder Array.filter oder Array.forEach verwenden.

Da Ihr Wert Array ist und Sie die Position des Elements benötigen, müssen Sie darüber iterieren.

Array.find

var data = [{"name":"placeHolder","section":"right"},{"name":"Overview","section":"left"},{"name":"ByFunction","section":"left"},{"name":"Time","section":"left"},{"name":"allFit","section":"left"},{"name":"allbMatches","section":"left"},{"name":"allOffers","section":"left"},{"name":"allInterests","section":"left"},{"name":"allResponses","section":"left"},{"name":"divChanged","section":"right"}];
var index = -1;
var val = "allInterests"
var filteredObj = data.find(function(item, i){
  if(item.name === val){
    index = i;
    return i;
  }
});

console.log(index, filteredObj);

Array.findIndex () @ Ted Hopp s Vorschlag

var data = [{"name":"placeHolder","section":"right"},{"name":"Overview","section":"left"},{"name":"ByFunction","section":"left"},{"name":"Time","section":"left"},{"name":"allFit","section":"left"},{"name":"allbMatches","section":"left"},{"name":"allOffers","section":"left"},{"name":"allInterests","section":"left"},{"name":"allResponses","section":"left"},{"name":"divChanged","section":"right"}];

var val = "allInterests"
var index = data.findIndex(function(item, i){
  return item.name === val
});

console.log(index);

Der Standardwert Array.indexOf() passt searchValue an das aktuelle Element und nicht an seine Eigenschaften. Sie können Array.indexOf - polyfill auf MDN referenzieren

19
Rajesh

Sie können Array.findIndex verwenden.

var data= [{
  "name": "placeHolder",
  "section": "right"
}, {
  "name": "Overview",
  "section": "left"
}, {
  "name": "ByFunction",
  "section": "left"
}, {
  "name": "Time",
  "section": "left"
}, {
  "name": "allFit",
  "section": "left"
}, {
  "name": "allbMatches",
  "section": "left"
}, {
  "name": "allOffers",
  "section": "left"
}, {
  "name": "allInterests",
  "section": "left"
}, {
  "name": "allResponses",
  "section": "left"
}, {
  "name": "divChanged",
  "section": "right"
}];
var index = data.findIndex(obj => obj.name=="allInterests");

console.log(index);

Ich hoffe es hilft :)

5
Sarat Chandra

In allen vorherigen Lösungen müssen Sie den Namen des Attributs oder Felds kennen. Eine allgemeinere Lösung für jedes Attribut ist:

let data = 
[{
    "name": "placeHolder",
    "section": "right"
}, {
    "name": "Overview",
    "section": "left"
}, {
    "name": "ByFunction",
    "section": "left"
}, {
    "name": "Time",
    "section": "left"
}, {
    "name": "allFit",
    "section": "left"
}, {
    "name": "allbMatches",
    "section": "left"
}, {
    "name": "allOffers",
    "section": "left"
}, {
    "name": "allInterests",
    "section": "left"
}, {
    "name": "allResponses",
    "section": "left"
}, {
    "name": "divChanged",
    "section": "right"
}]    

function findByKey(key, value) {
    return (item, i) => item[key] === value
}

let findParams = findByKey('name', 'allOffers')
let index = data.findIndex(findParams)

Durchlaufen Sie das Array und suchen Sie den Index des Elements, das einen Schlüssel name enthält und den Wert als übergebenen Parameter hat.

var data = [{
  "name": "placeHolder",
  "section": "right"
}, {
  "name": "Overview",
  "section": "left"
}, {
  "name": "ByFunction",
  "section": "left"
}, {
  "name": "Time",
  "section": "left"
}, {
  "name": "allFit",
  "section": "left"
}, {
  "name": "allbMatches",
  "section": "left"
}, {
  "name": "allOffers",
  "section": "left"
}, {
  "name": "allInterests",
  "section": "left"
}, {
  "name": "allResponses",
  "section": "left"
}, {
  "name": "divChanged",
  "section": "right"
}];

Array.prototype.getIndexOf = function(el) {

  var arr = this;

  for (var i=0; i<arr.length; i++){
     console.log(arr[i].name);
     if(arr[i].name==el){
       return i;
     }
     
  }
  
  return -1;

}

alert(data.getIndexOf("allResponses"));

0
void

Funktionsbasislösung zum Abrufen des Index von einem JSON-Objekt mit dem Wert von VanillaJS.

Beispiel: https://codepen.io/gmkhussain/pen/mgmEEW

    var data= [{
      "name": "placeHolder",
      "section": "right"
    }, {
      "name": "Overview",
      "section": "left"
    }, {
      "name": "ByFunction",
      "section": "left"
    }, {
      "name": "Time",
      "section": "left"
    }, {
      "name": "allFit",
      "section": "left"
    }, {
      "name": "allbMatches",
      "section": "left"
    }, {
      "name": "allOffers",
      "section": "left"
    }, {
      "name": "allInterests",
      "section": "left"
    }, {
      "name": "allResponses",
      "section": "left"
    }, {
      "name": "divChanged",
      "section": "right"
    }];
    
    
    // create function
    function findIndex(jsonData, findThis){
      var indexNum = jsonData.findIndex(obj => obj.name==findThis);  

    //Output of result
          document.querySelector("#output").innerHTML=indexNum;
          console.log("???? Array Index number: " + indexNum + " , value of " + findThis );
    }
    
    
    /* call function */
    findIndex(data, "allOffers");
Output of index number : <h1 id="output"></h1>
0
GMK Hussain

Sobald Sie ein JSON-Objekt haben

obj.valueOf(Object.keys(obj).indexOf('String_to_Find'))
0
Saurav