Javascript 仅使用chrome.storage.local.set更新指定元素
我有一个chrome本地存储阵列,格式:Javascript 仅使用chrome.storage.local.set更新指定元素,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我有一个chrome本地存储阵列,格式: {"flights": [ {"end":"2018-02-10","price":"476","start":"2018-02-01","tabId":1129367822}, {"end":"2018-02-11","price":"493","start":"2018-02-01","tabId":1129367825}, {"end":"2018-02-12","price":"468","start":
{"flights":
[
{"end":"2018-02-10","price":"476","start":"2018-02-01","tabId":1129367822},
{"end":"2018-02-11","price":"493","start":"2018-02-01","tabId":1129367825},
{"end":"2018-02-12","price":"468","start":"2018-02-01","tabId":1129367828}
]
}
现在,我以这种方式更新所有数据:
function updateValue(index, item) {
chrome.storage.local.get(['flights'], function (response) {
response.flights[index] = item;
chrome.storage.local.set({flights: response.flights});
});
}
function parseFlight(result) {
let flightsArray = [];
Object.keys(result).forEach(function (key) {
if (key.includes('flight')) {
let index = key.replace('flight_', '');
flightsArray[index] = result[key];
}
});
return flightsArray;
}
function updateValue(index, item) {
let flightPrefix = 'flight_';
let obj = {};
obj[flightPrefix + index] = item;
chrome.storage.local.set(obj);
}
chrome.storage.local.get(null, function (result) {
let flights = parseFlight(result);
});
但是异步请求有问题,因为我当时有几个请求。有些请求获取旧数据并将其再次保存在存储器中
我只想更新指定的元素(例如,使用新数据更新航班[0]),但它不起作用。。。
类似这样,但可行:
chrome.storage.local.set({flights[0]: item});
有没有办法做到这一点?或者你有一些建议,以其他方式解决这个问题
非常感谢您的帮助您可以将每个航班保存到单独的密钥中,并通过遍历所有存储获取所有航班:
cosnt flightPrefix = 'flight_';
function updateValue(index, item) {
chrome.storage.local.set({flightPrefix + index: item});
}
function getFlights() {
// Pass in null to get the entire contents of storage.
chrome.storage.sync.get(null, function(items) {
let flights = Object.keys(items).filter(key => key.beginsWith(flightPrefix));
console.log(flights);
});
}
您可以将每个航班保存到单独的密钥中,并通过遍历所有存储获取所有航班:
cosnt flightPrefix = 'flight_';
function updateValue(index, item) {
chrome.storage.local.set({flightPrefix + index: item});
}
function getFlights() {
// Pass in null to get the entire contents of storage.
chrome.storage.sync.get(null, function(items) {
let flights = Object.keys(items).filter(key => key.beginsWith(flightPrefix));
console.log(flights);
});
}
基于terales的回答(该代码有一些错误)。 我这样说:
function updateValue(index, item) {
chrome.storage.local.get(['flights'], function (response) {
response.flights[index] = item;
chrome.storage.local.set({flights: response.flights});
});
}
function parseFlight(result) {
let flightsArray = [];
Object.keys(result).forEach(function (key) {
if (key.includes('flight')) {
let index = key.replace('flight_', '');
flightsArray[index] = result[key];
}
});
return flightsArray;
}
function updateValue(index, item) {
let flightPrefix = 'flight_';
let obj = {};
obj[flightPrefix + index] = item;
chrome.storage.local.set(obj);
}
chrome.storage.local.get(null, function (result) {
let flights = parseFlight(result);
});
谢谢你的帮助 基于terales的回答(该代码有一些错误)。 我这样说:
function updateValue(index, item) {
chrome.storage.local.get(['flights'], function (response) {
response.flights[index] = item;
chrome.storage.local.set({flights: response.flights});
});
}
function parseFlight(result) {
let flightsArray = [];
Object.keys(result).forEach(function (key) {
if (key.includes('flight')) {
let index = key.replace('flight_', '');
flightsArray[index] = result[key];
}
});
return flightsArray;
}
function updateValue(index, item) {
let flightPrefix = 'flight_';
let obj = {};
obj[flightPrefix + index] = item;
chrome.storage.local.set(obj);
}
chrome.storage.local.get(null, function (result) {
let flights = parseFlight(result);
});
谢谢你的帮助 这当然是chrome.storage后端的一个缺点;您只有键值存储。如果你需要修改一个顶级值,你需要完整地读取它,编辑它,然后完整地写回去;您只有键值存储。如果需要修改顶级值,则需要完整读取、编辑并完整写回。