简体   繁体   English

比较数组,删除项目

[英]compare array, remove items

Using the Google Places API, you can only get 20 results back from a single query ... so you can't query for ALL results at once, but rather need to store what has already been searched for (in an array) and add to that array or remove from that array... 使用Google Places API,您一次查​​询只能返回20个结果……因此您无法一次查询所有结果,而是需要存储已搜索的内容(在数组中)并添加到该阵列或从该阵列中删除...

This demonstrates my question ... I need to be able to add places to my array, but then search through the array and remove any places that are no longer needed. 这证明了我的问题……我需要能够向阵列中添加位置,但是需要在阵列中搜索并删除不再需要的任何位置。

Is there a "compare array" function that I should be utilizing, or perhaps something better? 有没有我应该利用的“比较数组”功能,或者更好的功能? I just feel that's quite rudimentary... and perhaps I'm missing some Google API function that's obvious. 我只是觉得这很初级...也许我缺少一些显而易见的Google API函数。

My plan is to create an object: 我的计划是创建一个对象:

var placesObj = {};

And since all places can have multiple "types", I need to be able to push each place id into an array WITHIN the placesObj like so: 并且由于所有场所都可以具有多个“类型”,因此我需要能够将每个场所id推入一个在placesObj的数组中, placesObj所示:

var placesObj = {
    bars: {'123123123123123', '123123123355555', '12312312132123'},
    parks: {'123123123123123', '123123123355555', '12312312132123'}
};

This way, I can look for the id string and remove it from the entire placesObj ... 这样,我可以查找id字符串并将其从整个placesObj删除...

I hope this makes sense... I just need to know how to construct this object. 我希望这是有道理的...我只需要知道如何构造该对象即可。

http://php.net/manual/en/function.array-diff.php should be helpful for you. http://php.net/manual/zh/function.array-diff.php对您应该有所帮助。 It will return an array that has all the entries in array 1 that aren't present in the second array. 它将返回一个数组,其中包含第二个数组中不存在的所有数组1中的条目。 You should then be able to modify your arrays accordingly. 然后,您应该能够相应地修改阵列。

EDIT: Now that its clear that you're referencing javascript, not php, its a different conversation. 编辑:现在很明显,您引用的是javascript,而不是php,它的对话不同。 If I were going to compare JS objects and update them accordingly, I'd use key value pairs and delete like so: 如果我要比较JS对象并相应地更新它们,则可以使用键值对并像这样删除:

var placesObj = {
    bars: {'tavern': '123123123123123', 'bar123':'123123123355555', 'blahblah':'12312312132123'},
    parks: {'jungle' : '123123123123123', 'kidspar;':'123123123355555', 'foo': '12312312132123'}
}; 

delete placesObj['tavern']; 
delete placesObj['yourKey']; 

I'm assuming you're getting json responses from google and building this object from the response...you could probably just use some other unique value that you're getting from google to make the key like the place's name. 我假设您正在从google获取json响应,并从响应中构建此对象...您可能只需要使用从google获得的其他一些唯一值,就可以像地方名称一样创建密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM