0

I found a working script to add an id to each object;

function addId(id) {
    return function iter(o) {
        if ('fediverse' in o) {
            o.id = id++;
        }
        Object.keys(o).forEach(function (k) {
            Array.isArray(o[k]) && o[k].forEach(iter);
        });
    };
}

var data = [{"fediverse": "@username@mastodon.online", "name": "alternatenamehere", "alternate_names": "", "gender": "", "category": "", "description": "description here.", "link": "https://mastodon.online/users/username", "image": "https://files.mastodon.online/accounts/avatars/image.jpg", "language": "", "region": "", "user": true, "group": false, "creator": false, "companyOrganization": false, "project": false, "applicationSoftware": false}];

data.forEach(addId(1))
console.dir(data, {depth: null, colors: true, maxArrayLength: null});

This script outputs;

[
  {
    fediverse: '@username@mastodon.online',
    name: 'alternatenamehere',
    alternate_names: '',
    gender: '',
    category: '',
    description: 'description here.',
    link: 'https://mastodon.online/users/username',
    image: 'https://files.mastodon.online/accounts/avatars/image.jpg',
    language: '',
    region: '',
    user: true,
    group: false,
    creator: false,
    companyOrganization: false,
    project: false,
    applicationSoftware: false,
    id: 1
  }
]

the problem is the search I use detects ids from the first line only. how can I add the id at the first line instead of the last line?

Gwynne
  • 3
  • 3
  • Does it matter where you store id in an object. You always fetch with `.` or `[]`. You won't access it with `index`. – DecPK May 17 '21 at 03:36
  • This looks more like something you should be trying to fix in your search. Requiring attributes to be in certain lines in json will only cause pain and hair-tearing. – Infinite Recursion May 17 '21 at 03:36
  • That is not JSON -- [What is JSON anyway](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON) – Randy Casburn May 17 '21 at 03:37
  • @RandyCasburn it is a JSON object, you can stringify it into what you say it's JSON, but it's a valid and parsed javascript json object - in the browser, open console view (F12 f/example) and say `a = ` and dump that question object there, press ENTER, and now do `console.log(JSON.stringify(a))` and you will have your "JSON" – balexandre May 17 '21 at 07:13
  • Does this answer your question? [How to add a property at the beginning of an object in javascript](https://stackoverflow.com/questions/19457337/how-to-add-a-property-at-the-beginning-of-an-object-in-javascript) – balexandre May 17 '21 at 07:14
  • @decpk I'm using a search software called meilisearch, it says id has to be stored at the first line of an object or else it wouldn't run. – Gwynne May 17 '21 at 07:41
  • @balexandre kind of, I don't know much js so I'm having a hard time putting stuff into the code I already have for putting in ids. – Gwynne May 17 '21 at 08:07
  • @InfiniteRecursion It's meilisearch, They have their own rules for json data. – Gwynne May 17 '21 at 08:13
  • @balexandre - Incorrect or inaccurate use of terminology confuses people. I would like to encourage you to consider changing your use of these terms. This: "_it is a JSON object_" and this: "_javascript json object_" are inaccurate use of the term JSON. It is as simple as this: JSON is a string with formatting rules. There is no such thing as a JSON object or JavaScript JSON object. There are JSON strings and JavaScript Objects. – Randy Casburn May 17 '21 at 12:39
  • https://www.w3schools.com/Js/js_json_objects.asp what is this then? – Gwynne May 18 '21 at 11:18

0 Answers0