JavaScript data binding with an element of an array data binding with livedata android,data binding with fragments android,

WhY this notation does not work

const result1  = _model['dataarray[0]'];

_model.dataarray exists and it is an array with 2 elements

this "console.log(_model.dataarray[0])" works fines

Answer:1

Well console.log(_model.dataarray[0]) works fine because _model.dataarray[0] is different from _model['dataarray[0]'] which is incorrect, you shouldn't wrap the array index in the string.

Use the following syntax:

const result1  = _model['dataarray'][0];

Or like in the console.log():

const result1  = _model.dataarray[0];

Edit:

To have a function that dynamically gets a value from an object whther this value is a key or not, this how could be your code:

getValue = function(_model, valueToFind){
    if(_model.hasOwnProperty(valueToFind)){
        return _model[valueToFind];
    }else if(valueToFind.indexOf("[")>0){
        let key  = valueToFind.substring(0, valueToFind.indexOf("["));
        let index = parseInt(valueToFind.charAt(parseInt(valueToFind.indexOf("["))+1));
        return _model[key][index];
    }else{
        console.log('Please input a valid key!!');
    }
}

Demo:

getValue = function(_model, valueToFind){
    if(_model.hasOwnProperty(valueToFind)){
        return _model[valueToFind];
    }else if(valueToFind.indexOf("[")>0){
        let key  = valueToFind.substring(0, valueToFind.indexOf("["));
        let index = parseInt(valueToFind.substring(valueToFind.indexOf("[")+1, valueToFind.indexOf("]")));
        console.log(index);
        return _model[key][index];
    }else{
        console.log('Please input a valid key!!');
    }
}

var _model = {dataarray: ["test1", "test2"]};

console.log(getValue(_model, "dataarray"));
console.log(getValue(_model, "dataarray[1]"));
console.log(getValue(_model, "property"));
Answer:2

I'd like to combine a set of $.each statements looping through an array of arrays containing objects of k:v pairs into a single $.each which I can increment. Structure of array returned from PHP: an ...

I'd like to combine a set of $.each statements looping through an array of arrays containing objects of k:v pairs into a single $.each which I can increment. Structure of array returned from PHP: an ...

  1. increment array foreach

Suceeded in randomizing a quote but now the values name and quote don't match, is there a way to make json.name and json.quote share the same random value? so Einstein's quote isn't matched with the ...

Suceeded in randomizing a quote but now the values name and quote don't match, is there a way to make json.name and json.quote share the same random value? so Einstein's quote isn't matched with the ...

Something peculiar is happening for me with jquery-ui. I'm importing several pieces of jquery-ui separately in my HTML: <script type="text/javascript" src="../third-party/jquery-1.10.2/jquery.min....

Something peculiar is happening for me with jquery-ui. I'm importing several pieces of jquery-ui separately in my HTML: <script type="text/javascript" src="../third-party/jquery-1.10.2/jquery.min....

Say I have the following nested immutable data structure: import { Record, List } from 'immutable' const foo = List([ Record({ id: 1, gameState: Record({}) }) ...

Say I have the following nested immutable data structure: import { Record, List } from 'immutable' const foo = List([ Record({ id: 1, gameState: Record({}) }) ...