如何将数组中的值推送到铬存储中? [英] How to push value in array to store in chrome storage?

查看:33
本文介绍了如何将数组中的值推送到铬存储中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数组推送函数正在创建嵌套数组,而不是将值放入索引。它在项中创建嵌套项,而不是将其添加到数组的索引中。我试了很多方法,但没有找到问题所在。 我想在每个索引中添加数组,这样我就可以检索它并创建它的CSV。 我想要这样的输出:Item[0]=[标题,描述,价格,图像] 项目1=[标题、描述、价格、图片]等,根据输入的值 以下是我的js:

    $(function(){
        var title='';
            var price='';
            var description='';
            var image='';
            var i=0;
            var product =[];
            var products=[];
        $("#scrape").click(function(){
            chrome.storage.sync.get(["key"],function(result){
                if(result.key!=undefined){  
                    i=result.key;
                }
            });
        chrome.tabs.query({active:true,currentWindow:true},function(tabs){
            chrome.tabs.sendMessage(tabs[0].id,{todo:"fetch"},function(response){
                     description =response.description;
                     title=response.title;
                     image=response.image;
                     price=response.price;
                     product=[title,price,image,description];
                chrome.storage.sync.get(function(item){
                    if(item["products"] != undefined){
                        item["products"].push(product);
                    }
                    else{
                        item["products"]=product;
                    }
                    //console.log(item);
                    chrome.storage.sync.set({item});
                    i=i+1;
             });
            });
        });
    });
});

enter image description here

推荐答案

将存储视为具有键和值的标准JS对象。在您的例子中,使用像products这样的键是有意义的,它将是一个标准的JS数组。使用chrome.store的唯一概念区别在于,您需要分别读写该值。

$('#scrape').click(function () {
  chrome.tabs.query({active: true, currentWindow: true}, tabs => {
    chrome.tabs.sendMessage(tabs[0].id, {todo: 'fetch'}, response => {
      chrome.storage.sync.get('products', ({products = []}) => {
        products.push([
          response.title,
          response.price,
          response.image,
          response.description,
        ]);
        chrome.storage.sync.set({products});
      });
    });
  });
});

这篇关于如何将数组中的值推送到铬存储中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆