在下面,您可以看到我的NodeJS代码,在这里,我想打印与名称唯一的对象的值.

Array.prototype.uniqueArray = function (...props) {

  if (!props || (props instanceof Array && props.length === 0)) {
    return [...this]
  } else if (props instanceof Array===true) {
    return 'Invalid Parameter Type'
  }else{
    console.log("test 123")
  }
}

console.log([ ].uniqueArray('name') )
console.log([ {id: 1, name: "Jony"}, {id: 2, name: "Jony"}, {id: 3, name: "Kane"}].uniqueArray('name') )
console.log([ {id: 1, name: "Jony"}, {id: 2, name: "Jony"}, {id: 3, name: "Kane"}].uniqueArray('test') )

我想打印,如果我们输入返回相同空数组的空数组(如[]), 如果我们传递像‘name’这样的参数,它将根据‘name’返回唯一的数据. 这样我就可以正确地获得这些输出.

但是作为第三个‘console.log’部分,当我们传递没有包含在数组对象中的无效参数(如‘test’)时,我想返回"Inside参数类型".对于我可以在以下结果中得到的第三个‘console.log’部分:

发帖主题:Re:Kolibrios

[ { id: 1, name: 'Jony'} ] 

Expected 发帖主题:Re:Kolibrios

'Invalid Parameter Type'

推荐答案

一种方法可能是将对象重复数据消除到新数组中.如果该数组为空,您将知道在任何对象中是否没有找到该参数.

// Double check that uniqueArray doesn't exist
// on the array prototype
if (!('uniqueArray' in Array.prototype)) {
  
  // You're only sending in one argument to the
  // function so there's no need to spread it.
  Array.prototype.uniqueArray = function(prop) {
    
    // If there is no prop or or there is but it's not
    // a string, or the array is empty, return the array
    if ((!prop || typeof prop !== 'string') || !this.length) {
      return this;
    }

    // Iterate over the array of objects with reduce,
    // initialising it with a Map. This will hold our deduped
    // objects.
    const deduped = this.reduce((acc, obj) => {
      
      // Using the prop argument find the key which is
      // the value of that property in the object.
      const key = obj[prop];
      
      // If that key doesn't exist on the Map add it,
      // and set its value as the object
      if (key && !acc.has(key)) acc.set(key, obj);
      
      // Return the Map for the next iteration
      return acc;

    }, new Map());
    
    // Grab the values from the Map, and coerce them back
    // to an array
    const arr = [...deduped.values()];

    // If there are no objects in the array it means there were
    // no objects with values that matched the argument, so
    // return a message
    if (!arr.length) return 'Invalid Parameter Type';
    
    // Otherwise return the array
    return arr;
  
  }

}

console.log([ ].uniqueArray('name'));
console.log([ ].uniqueArray(23));
console.log([ {id: 1, name: "Jony"}, {id: 2, name: "Jony"}, {id: 3, name: "Kane"}].uniqueArray('name'));
console.log([ {id: 1, name: "Jony"}, {id: 2, name: "Jony"}, {id: 3, name: "Kane"}].uniqueArray('test'));

Javascript相关问答推荐

如何最好地从TypScript中的enum获取值

被CSS优先级所迷惑

角色 map 集/spritebook动画,用户输入不停止在键上相位器3

InDesign—创建一个独立的窗口,在文档中进行更正时保持打开状态

使用useEffect,axios和useParams进行react测试

如何从html元素创建树 struct ?

如何在输入元素中附加一个属性为checkbox?

我怎么才能得到Kotlin的密文?

用于在路径之间移动图像的查询

Nextjs 13.4 Next-Auth 4.2登录(&Quot;凭据&,{});不工作

如何在我的Next.js项目中.blob()我的图像文件?

在JavaScript中将Base64转换为JSON

是否设置以JavaScript为背景的画布元素?

如何在Reaction中清除输入字段

如何在Web项目中同步语音合成和文本 colored颜色 更改

如何将对象推送到firestore数组?

Reaction路由v6.4+数据API:重试加载程序而不显示错误

在不使用AJAX的情况下将JavaScript数组值传递给Laravel控制器?

使用JAVASCRIPT-使用If和Else If多次判断条件-使用JAVASRIPT对象及其属性

为什么我的AJAX联系人表单不能留在同一页上?