在Realtime Collaborative List上使用indexOf

时间:2016-12-13 20:18:44

标签: javascript google-realtime-api

我正在尝试学习Google云端硬盘实时Api,并对CollaborativeList提出疑问。我是JavaScript的新手。

var mylist = model.createList();
mylist.push("apple");
mylist.push("orange");
mylist.push("banana");
mylist.push("grape");

var result = mylist.indexOf("grape");

,结果是3,这是有道理的。现在,如果我创建一个由对象组成的列表:

var mylist = model.createList();

var apple = {"color": "red","time": "0"};
mylist.push(apple);

var orange = {"color": "orange","time": "1"};
mylist.push(orange);

var banana = {"color": "yellow","time": "2"};
mylist.push(banana);

var grape = {"color": "purple","time": "3"};
mylist.push(grape);

var result = mylist.indexOf(grape);

现在结果是-1。必须有一些我不理解的东西。我做错了什么?

2 个答案:

答案 0 :(得分:0)

Collaborative List不是Array#push的必要条件。它复制并包装对象,并不通过引用引用它。

当你使用像.indexOf()这样的协作列表mylist.indexOf(grape);时,你试图找到一个具有相同参考grape的对象,因为它不存在,它返回-1 }。

示例

在示例中,我们将对象推送到协作列表和JS数组。我们改变原始对象,并console.log()来自协作列表和数组的项目。正如您所看到的,协作列表中的对象没有更改(副本),而数组中的对象(参考)没有更改。

window.gapi.load('drive-realtime', start);

function start() {
  var doc = gapi.drive.realtime.newInMemoryDocument();

  var model = doc.getModel();

  var mylist = model.createList();
  var myArray = [];

  var apple = {
    "color": "red",
    "time": "0"
  };

  // push to Collaborative List
  mylist.push(apple);

  // puse to array
  myArray.push(apple);

  // mutate the original apple
  apple.color = 'green';
  
  console.log('origin: ', apple);

  console.log('Collaborative List: ', mylist.get(0));

  console.log('array: ', myArray[0]);
}
<script src="https://apis.google.com/js/api.js"></script>

答案 1 :(得分:0)

更改实时协作列表

感谢您的回答。总结一下:

看起来indexOf对于在Realtime中查找对象没有用 协作的对象列表。我需要更改列表中的一个对象 和思想indexOf将是一个快速的方法。所以它看起来像我必须 将Collaborative列表转换为javascript数组,遍历数组 找到我的对象,获取索引,然后更改 协作列表中的对象。

window.gapi.load('drive-realtime', startdemo);

function startdemo() {
var i;
var index;
/* Create an in memory document for testing. */
var doc = gapi.drive.realtime.newInMemoryDocument();

var model = doc.getModel();
var mylist = model.createList();

mylist.clear();

var apple = {"type": "apple","identifier": "100"};
mylist.push(apple);

var orange = {"type": "orange","identifier": "101"};
mylist.push(orange);

var banana = {"type": "banana","identifier": "102"};
mylist.push(banana);

var grape = {"type": "grape","identifier": "103"};
mylist.push(grape);

/* Convert the collaborative list to a javascript array. */
var myarray = mylist.asArray();

/* Show what is in the array */
for ( i = 0; i < myarray.length; i++ )
   {
   console.log("Original List, i = " +i +", type = " +myarray[i].type +", identifier = " +myarray[i].identifier);
   }

/* Find the index for our object. */
for ( i = 0; i < myarray.length; i++ )
   {
   if ( myarray[i].identifier === "103" )
      { 
      index = i;
      i = myarray.length;
      }
   }
console.log("index = " +index);

/* We will replace grape at identifier 103 with apricot. */
var apricot = {"type": "apricot","identifier": "103"};

mylist.set(index,apricot);

/* Get a new array to show the modification. */
myarray = mylist.asArray();

/* Now show what is in the list */
for ( i = 0; i < myarray.length; i++ )
   {
   console.log("Modified List, i = " +i +", type = " +myarray[i].type +", identifier = " +myarray[i].identifier);
   }
}
  
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>

更改Google实时协作列表

感谢您的回答。总结一下:

看起来indexOf对于在Realtime中查找对象没有用 协作的对象列表。我需要更改列表中的一个对象 和思想indexOf将是一个快速的方法。所以它看起来像我必须 将Collaborative列表转换为javascript数组,遍历数组 找到我的对象,获取索引,然后更改 协作列表中的对象。

window.gapi.load('drive-realtime', startdemo);

function startdemo() {
var i;
var index;

/* Create an in memory document for testing. */
var doc = gapi.drive.realtime.newInMemoryDocument();

var model = doc.getModel();
var mylist = model.createList();

mylist.clear();

var apple = {"type": "apple","identifier": "100"};
mylist.push(apple);

var orange = {"type": "orange","identifier": "101"};
mylist.push(orange);

var banana = {"type": "banana","identifier": "102"};
mylist.push(banana);

var grape = {"type": "grape","identifier": "103"};
mylist.push(grape);

/* Convert the collaborative list to a javascript array. */
var myarray = mylist.asArray();

/* Show what is in the array */
for ( i = 0; i < myarray.length; i++ )
   {
   console.log("Original List, i = " +i +", type = " +myarray[i].type +",    identifier = " +myarray[i].identifier);
   }

/* Find the index for our object. */
for ( i = 0; i < myarray.length; i++ )
   {
   if ( myarray[i].identifier === "103" )
      { 
      index = i;
      i = myarray.length;
      }
   }
console.log("index = " +index);

/* We will replace grape at identifier 103 with apricot. */
var apricot = {"type": "apricot","identifier": "103"};

mylist.set(index,apricot);

/* Get a new array to show the modification. */
myarray = mylist.asArray();

/* Now show what is in the list */
for ( i = 0; i < myarray.length; i++ )
   {
   console.log("Modified List, i = " +i +", type = " +myarray[i].type +", identifier = " +myarray[i].identifier);
   }

}

<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>