Use the Object.fromEntries method and pass the URLSearchParams instance as an argument. This creates a JavaScript object from the parsed query parameters: |
const params = Object.fromEntries( new URLSearchParams(window.location.search) ) // URL: example.com/path?foo=bar&name=futurestudio&num=1 // { foo: 'bar', name: 'futurestudio', num: '1' } |
Here’s how it works: the URLSearchParams class implements JavaScript’s iterator interface. The iterator interface contains the Iterator#entries method. This entries methods returns an iterator providing an array of [key, value] pairs: |
const params = new URLSearchParams('?foo=bar&name=futurestudio&num=1') const iterator = params.entries() iterator.next() // { done: false, value: ['foo', 'bar'] } iterator.next() // { done: false, value: ['name', 'futurestudio'] } iterator.next() // { done: false, value: ['num', '1] } iterator.next() // { done: true, value: undefined } |
Showing posts with label object. Show all posts
Showing posts with label object. Show all posts
Tuesday, February 7, 2023
Convert URLSearchParams to a JavaScript Object | JavaScript jQuery Get Parameters from URL | Get Params from URL using JavaScript
Saturday, December 17, 2016
How to convert Map / Object to Bytes and save to internal storage and read back
package com.pkm; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Map; /** * * @author PRITOM */ public class MapObjectToByteArrayAndSaveToFile { public static void main(String[] args) throws Exception { Map<Integer, String> data = new HashMap<Integer, String>(); data.put(1, "hello"); data.put(2, "world"); System.out.println(data.toString()); store(data); data = (Map<Integer, String>) load(); System.out.println(data.toString()); } public static Object load() throws Exception { FileInputStream fileInputStream = new FileInputStream("object.txt"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); return objectInputStream.readObject(); } public static void store(Object data) throws Exception { FileOutputStream fileOutputStream = new FileOutputStream("object.txt"); ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream); outputStream.writeObject(data); outputStream.close(); } }
Wednesday, July 17, 2013
PHP: Recursively convert an object to an array
When pulling in array from outside data sources you often receive an array back. The problem is that sometimes even though you know you should have an array your application does not and therefore assigns it to the stdObject object, which of course is nothing. To make it usable you must convert it back into an array. With a simple object that may be as simple as a cast, but when you are working with large complex datasets that may be several layers deep you need to make sure you get at all of them. Enter beautiful recursion. After playing with PHPs array_walk_recursive() for a bit I hit on a custom recursive function that does exactly the job. Simply pass it your object and it will munch away at it trying to convert it into a PHP array. Take a gander at the code for this below.
http://ben.lobaugh.net/blog/567/php-recursively-convert-an-object-to-an-array
<?php
function object_to_array($obj)
{
if (is_object($obj)) {
$obj = (array) $obj;
}
if (is_array($obj)) {
$new = array();
foreach ($obj as $key => $val) {
$new[$key] = object_to_array($val);
}
} else {
$new = $obj;
}
return $new;
}?>
http://ben.lobaugh.net/blog/567/php-recursively-convert-an-object-to-an-array
Subscribe to:
Posts (Atom)