How do I apply my Vanilla JS Array manipulation method in ReactJS - javascript

I am finishing up my REST API and I am trying to convert an array I received from my backend into a Nested Object of Objects or an array of objects, whatever can help me avoid using direct indices to access elements.
I have tried converting the array but I only receive a single object which is one of the many arrays in the multidimensional array. I need it to convert the whole dataset, not to only return one entry.

I took the function designed for a map function of the array object with Object.assign() and placed in the method I am using in my Express backend and sent the data to my react client as an array of already converted Objects.
Mind you this data was received in CSV format.

Related

Sort one array based on another array (Proxy from mobx's store) on React.js

What i'm trying to do
I want to go through one array, create another array that represents mapping by index, save it in Local Storage, and then when the app first mounts to check if there is a value stored in Local storage, and if so then sort the array of data coming from the store (as Proxy) So that they are displayed according to the mapping in LocalStorage.
Hope the explanation is clear :)
I got the data from store as a Proxy like so:
const chocolates = machineStore?.SubsystemsInfo
when i console it out, i need to stringify and parse it so i can view it in the console. it look like so:
When the component mounts, i get the array of mapped indexes from LocalStorage,
but the problem now is that i need to render the original array based on it and i cant because it's a Proxy and i cant get access to the array itself, so i can then sort.
how can i convert the data to a regular array so i can work with it ?
i already tried spreading, and parsing so it brings am empty array.
any ideas what do i miss?
Thanx

When using AJAX, unable to split string and treat it as an array

I'm trying to use AJAX to receive updates from a database without needing to reload the page. I've had very minimal problems thus far, however, I finally hit one. When retrieving information from a database, I have it convert all possible rows to an array. From there, I encode that array back to Javascript using $test1 = json_encode($array). In this same PHP function, I'm doing this to multiple pieces, converting those encoded arrays to a string by doing echo $test1."#".$test2."#".$test3. In Javascript, I split that string by the hashtag, and then grab the information of an array by doing something like test1 = array[1]. When doing that, I can retrieve that array entry as a string even though it's in the fashion of a Javascript array. Any ideas on how I can make it treat that string, which is setup like an array, as an array? Thanks in advance!
When retrieving the information from the AJAX response, you need to make it a Javascript Object. Convert the response to an object by doing JSON.parse(response) to get it to work

Can I use SQL to store my Javascript objects?

I'm new to programming, and I have been programming a small project with vanilla javascript, but I was using a lot of document.getElementById() tags, and I stored all of these in a javascript object, on a seperate file, but I was wondering If I could Just store that object on a SQL file, to make my project more organized.
I'm not sure if that's possible, I know that SQL stores data, so would I be able to store my JS object on a sql file, and import that object into my seperate Javascript files?
I'm trying to make sure if I can do what I want to do before I decide to start learning sql, but If it does do what I need, I was going to start incorporating it for organization, so I can learn it as I create projects.
You can use the JSON.stringify function to convert your javascript objects into strings. However, it is important to note that the only items within the javascript object that are converted into strings are: objects, arrays, strings, numbers, and values that are: null, true, or false. If you have references to functions or classes that have been instantiated, then these will be lost. You can convert the string back into a javascript object using JSON.parse.
One thing to consider before you do this is whether or not you need to perform database queries on the data that you are storing within the javascript object. If you need to search on the javascript object's data, then you should store the information directly within tables in the database. If you don't need to search on it, then converting the data to a string and saving it should be fine to do. Since it sounds as though you are using the data for your own purposes, doing this should be fine since extracting all of the data from the database shouldn't be an intensive task. Also, you can write your own scripts to parse the data.
Definitely, you can store as a JSON Blob
https://learn.microsoft.com/en-us/sql/relational-databases/json/store-json-documents-in-sql-tables?view=sql-server-ver15

Getting the Value using an index on an Object Hashmap

As far as i know, unlike arrays object hashmaps use literal keys to query the value, Is it possible to query the object hashmap using an index like one would do on arrays? I know this is possible using frameworks like underscore.js but i just want a vanilla javascript method if by any chance its possible.
Basically Arrays are the kind of data structure which is defined loosely as collection of values (may be of different types in JS) whereas Object or hashmaps (traditionally) are a record of key-value pairs i.e. a value mapped to a key.
So in Arrays can be accessed by an index, but Objects are a type in themselves. But you surely can have an array of Objects which you can access by index.
Hope this answers your query.

Using jQuery to set/retrieve multiple key/value pairs in one cookie using an array

My goal here is to cut down the number of cookies I'm using to store things like persistent states into a single cookie by setting and retrieving multiple key/value pairs within a single cookie using an array.
I'm mostly interested in knowing if someone else has already done this and has written a plugin for it, as I would think this has been done before.
You could serialize the object into a JSON string. That would make it super simple to re-load the object. See question 191881 for information on how to serialize an object to JSON in JavaScript.

Categories