【问题标题】:How can i convert my arrays into object or json webscraping [duplicate]我如何将我的数组转换为对象或 json webscraping [重复]
【发布时间】:2021-06-09 19:41:16
【问题描述】:

转换抓取数据数组的最佳方法是什么:

[ 'random product 1', 'random product 2', 'random product 3', 'random product 4'] [ '$99', '$99', '$99', '$99']


                                              into object 

{ name: 'random product 1', price: '$100', }{ name: 'random product 2', price: '$100', }{ name: 'random product 3', price: '$100', }


我试过了

var keys = [
        'name',
        'price'
    ]
const mainData = {} 
    for (i = 0; i < 10; i++) {
        let keyIdx = 0;
        for (j = 0; j < 2; j++) {
            if (l < 2) {
            //bundle.name and bundle.price contains above mentioned array of name and price
                mainData[keys[j]] = bundle.name[l];
                // keyIdx++;
                j++;
                mainData[keys[j]] = bundle.price[l];
            }
            

但它只返回一个对象{ name: 'random product 3', price: '$99' }


【问题讨论】:

标签: javascript arrays json object web-scraping


【解决方案1】:

如果您有 2 个长度相同的数组,您可以映射其中一个数组,并使用 iname 与对应的 price 链接起来。

 const names = ['random product 1', 'random product 2', 'random product 3', 'random product 4']
 const prices = ['$99', '$99', '$99', '$99']


 const result = names.map((name, i) => {
   return {
     name,
     price: prices[i]
   }
 });

 console.log(result);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2020-02-06
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多