2

I'm making a project with Xel, but I am stuck on a problem.

I want to loop through the keys of an object, and here's a minimal example of what I'm trying to get:

const strings = import("xel:strings")

let obj = {
    key1: "value",
    key2: "value2,"
    key3: "value3"
}

let i = 0
// This "keys" function is hypothetical. I'm not sure what I should do here.
// "keys" might return an array
const allKeys = keys(obj)
while ( len(allKeys) > i ) {
    const key = allKeys[i]
    const value = obj[key]
    print(strings.format("%v: %v", key, value))
    i = i + 1
}

But this doesn’t seem to work... How can I achieve this?

2
  • What programming language? Commented May 22 at 14:49
  • @PeterMortensen Thanks for your interest, this is Xel Commented May 22 at 14:56

2 Answers 2

1

In Xel, iterating over the keys of an object is a common requirement, especially when working with dynamic data structures. While earlier versions lacked native support for this functionality, recent updates have addressed the gap.

Before Xel v0.13.0, looping over object keys in Xel was unfortunately not directly possible due to the lack of built-in object manipulation functions. However, I'm happy to announce that Xel v0.13.0 (released in 9th June 2025) introduces the xel:object module, which makes this task straightforward!

Here's how you can achieve your goal using the new features:

Solution


const object = import("xel:object")
const strings = import("xel:strings")

let obj = {
    key1: "value",
    key2: "value2",
    key3: "value3"
}

const allKeys = object.keys(obj) // returns an array of keys

let i = 0

while (len(allKeys) > i) {
    const key = allKeys[i]
    const value = object.get(obj, key) // or obj[key]

    print(strings.format("%v: %v", key, value))
    i = i + 1
}

Explanatuon:

  1. object.keys(obj): This function from the xel:object module takes your object obj and returns an array containing all of its keys (property names).

  2. object.get(obj, key): We now use object.get(obj, key) to retrieve the value associated with a given key.

Alternative Method:

You can also use the object.entries() function and a forEach loop for a more concise solution:


const object = import("xel:object")
const strings = import("xel:strings")
const array = import("xel:array")

let obj = {
    key1: "value",
    key2: "value2",
    key3: "value3"
}

const entries = object.entries(obj) // returns an array of [key, value] pair

array.forEach(entries, (entry) {
    const key = entry[0]
    const value = entry[1]

    print(strings.format("%v: %v", key, value))
})

This method uses object.entries() to get an array where each element is an array containing the key and value, then iterates over the entries using array.forEach() (introduced in earlier Xel releases).

Sign up to request clarification or add additional context in comments.

Comments

0
const strings = import("xel:strings")

let obj = {
    key1: "value",
    key2: "value2",
    key3: "value3"
}

let keys = map.keys(obj)
let i = 0

while (i < len(keys)) {
    let key = keys[i]
    let value = obj[key]
    print(strings.format("%v: %v", key, value))
    i = i + 1
}


map.keys(obj) — retrieves an array of keys from the map-like object.

len(keys) — returns the number of keys.

obj[key] — accesses the value dynamically using the key string.

strings.format() — formats the string output.

Another method
As of current Xel (or XelScript) behavior, there is no map.keys() function built-in. Instead, to loop over keys of an object, Xel provides a native for loop syntax for objects.

strings = import("xel:strings")

obj = {
    key1: "value",
    key2: "value2",
    key3: "value3"
}

for (let key in obj) {
    let value = obj[key]
    print(strings.format("%v: %v", key, value))
}

2 Comments

Thanks for your answer! But that does not resolve the issue. I am getting this error when I try that: Error: Runtime Error: Cannot resolve variable map
Appreciate the edit! Sadly, I get: Error: Syntax Error: Expected Primary Expression, got 'Let' from L9C6 to L9C9.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.