更改实时协作列表
谢谢你的回答。总结一下:
看来 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>