-1

我正在尝试学习 Google Drive Realtime 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。一定有什么我不明白的。我究竟做错了什么?

4

2 回答 2

0

更改实时协作列表

谢谢你的回答。总结一下:

看来 indexOf 对于在对象的实时协作列表中查找对象没有用。我需要更改列表中的一个对象,并认为 indexOf 将是一种快速方法。所以看起来我必须将协作列表转换为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 对于在对象的实时协作列表中查找对象没有用。我需要更改列表中的一个对象,并认为 indexOf 将是一种快速方法。所以看起来我必须将协作列表转换为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>
于 2016-12-14T22:32:38.773 回答
0

协作列表与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>

于 2016-12-13T20:53:42.453 回答