Why can't I .push() in an array of paths of outlinks? Can someone explain why my code doesn't work?

What I’m trying to do

I want to add the current file path at the end of a list of its outlinks paths (or at the start)

example: test1 is the current note, test2 and test3 are the outlink notes. What I want in the final array:

  • test2.md
  • test3.md
  • test1.md

Things I have tried

this is the code I ran that gives the error: “notes.push is not a function”:

```dataviewjs
// get all outlinks paths
let notes = dv.current().file.outlinks
.map(x => x.path);

// push current path to notes
notes.push(dv.current().file.path);

// print notes
dv.paragraph("printing:");
dv.paragraph(notes);
```

I have checked and confirmed that notes is an array:

dv.paragraph("is this array:");
dv.paragraph(dv.isArray(notes));

I have also tried to see what happens if I ran dv.array(notes) function and then ran notes.push() which still outputs the same error.

For now, I found a workaround is to create an empty array and push the elements of notes and then push the current file path:

```dataviewjs
// get all outlinks paths
let notes = dv.current().file.outlinks
.map(x => x.path);

// create empty array
let arr = [];

// push notes element to empty array
notes.map(y => arr.push(y));
arr.push(dv.current().file.path);

// print array
dv.paragraph("printing:");
dv.paragraph(arr);
```

My question:

Why can .push() work for an array but not another?

Because it’s not a standard Javascript array :wink: It’s a “Dataview array”.

From the docs:

If you want to convert a Data array back to a normal array, use DataArray#array()

So you use this code, and add the .array() in after your .map:

```dataviewjs
// get all outlinks paths
let notes = dv.current().file.outlinks
.map(x => x.path)
.array()

// push current path to notes
notes.push(dv.current().file.path);

// print notes
dv.paragraph("printing:");
dv.paragraph(notes);
```
1 Like

Ah! that worked! Thank you!! all this time I have been trying to do dv.array() like an iziot :smile:

Thank you for your answer!! :blush:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.