Loops how to iterate? "Solved"

Hi I am looking to see if someone knows how to create an array of returned blocks but with specific titles within the blocks? so for example below, I have a code that gets the results of a query which has 30 items within all with different timestamps, last hash, hash , data , nonce, difficulty values. I would like to return an array of the 30 items with only the returned data outlined above? I have tried to split the return results from the query defining the titles and then looking to create the individual blocks with their own individual data, is this possible and if so how would it be done?

function getBlocks(){
wixData.query("Blocks")
        .ascending("_createdDate")
        .find()
        .then((results) => {
 let block = results.items;
 for (let i = 0; i < block.length; i++) {
                console.log(block[i]);

 let timestamp = block.timeStamp;
 let lastHash = block.lastHash;
 let hash = block.hash;
 let data = block.data;
 let nonce = block.nonce;
 let difficulty = block.difficulty;

                block[i] = {
 "Time Stamp": timestamp,
 "Last Hash": lastHash,
 "Hash": hash,
 "Data": data,
 "Nonce": nonce,
 "Difficulty": difficulty
                }

                console.log(block[i]);

 return block[i];
            }
        })
}

You´re almost there, I think, just this line:

block[i] = {
 "Time Stamp": timestamp,

is assigning new values o an existing array. If you want a clean array, just define a new one (let newArray = []:wink: and replace block with newArray.
P.S. You are basically recreating the javascript .map function.

Hi Giri,

I thank you for your answer,

I tried to tidy up the code a little bit yesterday, and this is what I have come up with so far:

wixData.query("Blocks")
        .ascending("_createdDate")
        .find()
        .then((results) => {
 for (let i = 0; i < results.items.length; i++) {
 
 let blocks = {
 "Time Stamp": results.items[i].timeStamp,
 "Last Hash": results.items[i].lastHash,
 "Hash": results.items[i].hash,
 "Data": results.items[i].data,
 "Nonce": results.items[i].nonce,
 "Difficulty": results.items[i].difficulty
                }

                console.log(blocks);

 return blocks;
            }
        })
}

I am aware of the map function but I have not implemented it as of yet, any guidance would be great, I will be looking to resolve this issue with your suggestions, I am currently in mid development of a blockchain and the majority of it is working, it is just me trying to get it understood by WiX, as I am using the data collections to store the blocks and the transactions.

I am then looking at how to utilise the Wix realtime API to communicate between peers to update/refresh their datasets when new transactions and blocks are added.

best wishes,

Si

Just looked in to javascript map and this is what I have come up with, it does not seem to be creating a new array of all blocks just the first one?

function getBlocks() {
    wixData.query("Blocks")
        .ascending("_createdDate")
        .find()
        .then((results) => {
 var arr = [{ results }];

 for (let i = 0; i < results.length; i++) {

 let newArray = [arr.map(block => ({
                    timestamp: results.items[i].timeStamp,
                    lastHash: results.items[i].lastHash,
                    hash: results.items[i].hash,
                    data: results.items[i].data,
                    nonce: results.items[i].nonce,
                    difficulty: results.items[i].difficulty
                }))];

                console.log(newArray[i]);

 return newArray;
            }
        })
}

Look again at the .map docs. It iterates over an entire array:

The map() method creates a new array with the results of calling a function for every array element.

Source: JavaScript Array map() Method
So there is no need, unless I´m not getting it, to put it inside a for-loop as well.

I would like to remind you that this is not a Corvid question, this is a Javascript-question. You could have put this on Stackoverflow too.

Hi Giri,

it may not look like a corvid question but for me I am trying to utilise the Wix data api, when all data is returned in the query it brings up the ID, owner, date created, date updated etc, for me I do not want to return all this data, I simply want to return an array of the data that I want that includes timestamp, lastHash, hash, data, nonce, difficulty for each result from the query, in an order that I can use for the next part of the work I am doing, so I do agree that it seems to be a javascript question but it is also the fact that Wix data api returns all info when only needing specifics from a query result array. I checked the map piece and have tried to implement a number of ways and have yet not achieved the array of results in the way i need them.

Si

Hi All,

Just to keep you all updated, I have managed to figure out how to loop through and create the response or to draw the responses need out of an Array.

I provide the code that I have found to work for me below, I hope someone else finds this useful.

async function getAllBlocks() {
 const data = await wixData.query("Blocks").ascending("_createdDate").find();
 return data.items;
}

async function initialiseNewBlocks() {
 let newBlocks = [];
 const blocks = await getAllBlocks();

    blocks.map(function(item) {
 let trimmedBlock ={
 "timeStamp" : item.timeStamp,
 "lastHash" : item.lastHash,
 "hash": item.hash,
 "data": item.data,
 "difficulty" : item.difficulty, 
 "nonce": item.nonce
        }
        newBlocks.push(trimmedBlock);
    });

 return newBlocks;
}

Well done!

@russian-dima thank you for your kind comments, I am however now having to try and get the result of the map, within a class, for example:

import wixData from 'wix-data';
import { Block } from 'public/block.js';

export class Blockchain {
    constructor() {
 this.chain = (async function () {
 await initialiseBlocks().then(function (result) {
                console.log(result);
 return result;
            })
        })
    }

However, what seems to be happening that when in client side “let chain = new Blockchain()” console.log on chain returns a chain array with “null”, which I assume is to do with a timing issue as the initial code would have to run before the call of Chain however it isn’t working? now I know there must be a work around, but it is currently out of my knowledge base. This I know sounds like a javascript question but it stems back to the fact that I am wanting to only retrieve certain data from the Wix data api.

Si

@simonadams
Sorry, here i am out. This is Giris part :grin: